-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
149 lines (128 loc) · 3.88 KB
/
mainwindow.cpp
File metadata and controls
149 lines (128 loc) · 3.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "settingslider.h"
#include <QSpacerItem>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
wsdata = new WSData( 800, 600 );
ws = new WorkspaceWidget(this, this);
comb = new Comb(5, 50);
combns = new CombNS(5, 50);
scaler = new Scaler(3, 60);
setCurTool( comb );
ws->loadImage(QString("img.jpg"));
ui->mainLayout->addWidget(ws);
this->connect(ui->actionSave,SIGNAL(activated()),SLOT(saveToFile()));
this->connect(ui->actionOpen,SIGNAL(activated()),SLOT(loadFromFile()));
this->connect(ui->actionOpenImage,SIGNAL(activated()),SLOT(loadImageFromFile()));
this->connect(ui->actionExit,SIGNAL(activated()),SLOT(saveAndQuit()));
}
void MainWindow::saveAndQuit()
{
QMessageBox::StandardButton reply;
reply = QMessageBox::question(this, "Save", "Do You want to save changes?",
QMessageBox::Yes|QMessageBox::No);
if (reply == QMessageBox::Yes)
{
saveToFile();
}
close();
}
void MainWindow::loadImageFromFile()
{
QMessageBox::StandardButton reply;
reply = QMessageBox::question(this, "Save", "Do You want to save changes?",
QMessageBox::Yes|QMessageBox::No);
if (reply == QMessageBox::Yes)
{
saveToFile();
}
QString fname = QFileDialog::getOpenFileName();
if(fname == QString(""))
return;
ws->loadImage(QString(fname));
update();
}
void MainWindow::saveToFile()
{
QString fname = QFileDialog::getSaveFileName();
if(fname == QString(""))
return;
FILE* f;
f = fopen( fname.toUtf8().constData(), "wb" );
int w, h;
size_t s;
w = wsdata->width();
h = wsdata->height();
s = sizeof(vec2);
fwrite( &w, sizeof(int), 1, f );
fwrite( &h, sizeof(int), 1, f );
fwrite( &s, sizeof(size_t), 1, f);
fwrite( wsdata->getData(), sizeof(vec2), wsdata->width() * wsdata->height(), f );
fclose(f);
}
void MainWindow::loadFromFile()
{
QMessageBox::StandardButton reply;
reply = QMessageBox::question(this, "Save", "Do You want to save changes?",
QMessageBox::Yes|QMessageBox::No);
if (reply == QMessageBox::Yes)
{
saveToFile();
}
QString fname = QFileDialog::getOpenFileName();
if(fname == QString(""))
return;
FILE* f;
f = fopen( fname.toUtf8().constData(), "rb" );
int w, h;
size_t s;
fread( &w, sizeof(int), 1, f );
fread( &h, sizeof(int), 1, f );
fread( &s, sizeof(size_t), 1, f);
if( wsdata )
delete wsdata;
wsdata = new WSData( w, h );
fread( wsdata->getData(), sizeof(vec2), wsdata->width() * wsdata->height(), f );
fclose(f);
update();
}
void MainWindow::setCurTool( Tool *nt )
{
curtool = nt;
curtool->setData(wsdata);
QLayoutItem *item;
while( ( item = ui->settingGroup->layout()->takeAt(0) ) != NULL )
{
delete item->widget();
delete item;
}
SettingList sm = curtool->getSettings();
for( SettingList::iterator i = sm.begin(); i != sm.end(); ++i )
{
SettingSlider* ss = new SettingSlider(this);
ss->setSetting( (*i) );
ui->settingGroup->layout()->addWidget( ss );
this->connect(ss, SIGNAL(updateSetting( const Setting& )), SLOT(updateTool( const Setting& )) );
}
}
void MainWindow::updateTool(const Setting &s)
{
curtool->setSetting(s);
}
void MainWindow::selectComb(bool v) { if( v ) setCurTool( comb ); }
void MainWindow::selectCombNS(bool v) { if( v ) setCurTool( combns ); }
void MainWindow::selectScaler(bool v) { if( v ) setCurTool( scaler ); }
WSData* MainWindow::getWSData() { return wsdata; }
Tool* MainWindow::getTool() { return curtool; }
MainWindow::~MainWindow()
{
delete ui;
delete wsdata;
delete ws;
delete comb;
delete scaler;
}