forked from seub/CirclePackings
-
Notifications
You must be signed in to change notification settings - Fork 0
/
top_menu.cpp
360 lines (299 loc) · 11.2 KB
/
top_menu.cpp
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/*This file is part of Circle Packings.
Circle Packings is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Circle Packings is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Circle Packings. If not, see <http://www.gnu.org/licenses/>.*/
#include "top_menu.hpp"
#include <QMenu>
#include <QMessageBox>
#include <QFileDialog>
#include <QDialog>
#include <QLineEdit>
#include <QGridLayout>
#include <QLabel>
#include <QComboBox>
#include <QSpinBox>
#include <QPushButton>
#include <QCheckBox>
#include "window.hpp"
#include "canvas.hpp"
#include "canvas_delegate.hpp"
#include "input_menu.hpp"
#include "output_menu.hpp"
#include "configuration.hpp"
#include "help_browser.hpp"
Top_Menu::Top_Menu()
{
}
Top_Menu::~Top_Menu()
{
}
Top_Menu::Top_Menu(Window* window)
{
setParent(window);
window_ = window;
file_menu_ = new QMenu(tr("&File"), this);
save_action_ = file_menu_->addAction(tr("&Save"),this, SLOT(save()), QKeySequence(Qt::CTRL + Qt::Key_S));
save_as_action_ = file_menu_->addAction(tr("Save &as..."),this, SLOT(save_as()), QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_S));
load_action_ = file_menu_->addAction(tr("&Open..."),this, SLOT(open()), QKeySequence(Qt::CTRL + Qt::Key_O));
print_action_ = file_menu_->addAction(tr("&Print..."),this, SLOT(print()), QKeySequence(Qt::CTRL + Qt::Key_P));
exit_action_ = file_menu_->addAction(tr("E&xit"),window_, SLOT(close()), QKeySequence(Qt::CTRL + Qt::Key_Q));
addMenu(file_menu_);
setting_menu_ = new QMenu(tr("&Settings"), this);
load_image_action_ = setting_menu_->addAction(tr("&Set image..."),window_, SLOT(load_image()), QKeySequence(Qt::CTRL + Qt::Key_I));
addMenu( setting_menu_);
help_menu_ = new QMenu(tr("&Help"), this);
help_action_ = help_menu_->addAction(tr("&Help"),this,SLOT(help()), QKeySequence(Qt::Key_F1));
about_action_ = help_menu_->addAction(tr("&About"), this, SLOT(about()));
addMenu(help_menu_);
show_save_image_warning_ = true;
}
void Top_Menu::print()
{
print_dialog_ = new QDialog;
print_dialog_->setWindowTitle("Print in file...");
QGridLayout *layout = new QGridLayout;
QLabel *canvas_label = new QLabel;
canvas_choice_ = new QComboBox;
QLabel *quality_label = new QLabel;
quality_spinbox_ = new QSpinBox;
QLabel *file_label = new QLabel;
QPushButton *browse_button = new QPushButton;
print_button_ = new QPushButton;
QPushButton *cancel_button = new QPushButton;
file_name_ = new QLineEdit;
file_name_->setText("CP.png");
canvas_label->setText(tr("Canvas: "));
canvas_choice_->addItem("left", 0);
canvas_choice_->addItem("right",1);
quality_label->setText(tr("Quality: "));
quality_spinbox_->setRange(0,100);
quality_spinbox_->setValue(80);
file_label->setText("Filename: ");
browse_button->setText(tr("Browse..."));
print_button_->setText(tr("Print"));
cancel_button->setText(tr("Cancel"));
connect(browse_button,SIGNAL(clicked()),this,SLOT(browse()));
connect(cancel_button,SIGNAL(clicked()),print_dialog_,SLOT(close()));
connect(print_button_,SIGNAL(clicked()),this,SLOT(print_in_file()));
connect(file_name_,SIGNAL(textChanged(QString)),this,SLOT(print_file_name_changed(QString)));
layout->setVerticalSpacing(15);
layout->setRowMinimumHeight(3, 20);
layout->setColumnMinimumWidth(4, 60);
layout->addWidget(canvas_label,0,0,1,1);
layout->addWidget(canvas_choice_,0,1);
layout->addWidget(quality_label,0,5,1,1,Qt::AlignRight);
layout->addWidget(quality_spinbox_,0,6,1,1);
layout->addWidget(file_label,2,0,1,1);
layout->addWidget(file_name_,2,1,1,5);
layout->addWidget(browse_button,2,6,1,2);
layout->addWidget(print_button_,4,6,1,2);
layout->addWidget(cancel_button,4,5,1,1);
print_dialog_->setLayout(layout);
print_button_->setDefault(true);
print_dialog_->show();
}
void Top_Menu::print_in_file()
{
bool success = false;
if(canvas_choice_->currentIndex()==0)
{
success = window_->left_canvas_->print_in_file(file_name_->text(),quality_spinbox_->value());
}
else
{
success = window_->right_canvas_->print_in_file(file_name_->text(),quality_spinbox_->value());
}
if (success)
{
print_dialog_->close();
}
else
{
QMessageBox message_box_1;
message_box_1.setWindowTitle("Print...");
message_box_1.setIcon(QMessageBox::Warning);
message_box_1.setText("Failed to print the image with the given extension.");
message_box_1.setInformativeText("Do you want to retry?");
message_box_1.setStandardButtons(QMessageBox::No | QMessageBox::Yes);
message_box_1.setDefaultButton(QMessageBox::Yes);
int retour = message_box_1.exec();
switch(retour)
{
case QMessageBox::Yes :
return;
break;
case QMessageBox::No :
print_dialog_->close();
break;
default:
std::cout << "ERROR in Top_Menu::print_in_file: should never have reached here" << std::endl;
throw(QString("ERROR in Top_Menu::print_in_file: should never have reached here"));
}
}
return;
}
void Top_Menu::print_file_name_changed(QString filename)
{
print_button_->setDisabled(filename.isEmpty());
return;
}
void Top_Menu::browse()
{
QString new_file_name =
QFileDialog::getSaveFileName(print_dialog_,
tr("Print in file..."),
"CP.png",
tr("PNG files (*.png);; JPG files (*.jpg);;All Files (*)"));
if (!new_file_name.isEmpty())
{
file_name_->setText(new_file_name);
}
print_button_->setDefault(true);
return;
}
void Top_Menu::save_as()
{
if (window_->mode_flag_ == DRAW_2_CURVES && window_->input_menu_->choose_coloring_box_2_->currentIndex() == T_IMAGE
&& show_save_image_warning_)
{
QMessageBox message_box;
message_box.setWindowTitle("Save...");
message_box.setIcon(QMessageBox::Information);
QString message = QString("Saving the image is not supported.");
message_box.setText(message);
message_box.setInformativeText("You may need to set an image again when you load the file.");
message_box.setStandardButtons(QMessageBox::Ok);
message_box.exec();
show_save_image_warning_ = false;
}
last_loaded_file_ =
QFileDialog::getSaveFileName(this,
tr("Save as..."),
(last_loaded_file_.isEmpty()?"save.cp":last_loaded_file_.toStdString().c_str()),
tr("CP files (*.cp);;All Files (*)"));
if(last_loaded_file_.isEmpty())
{
return;
}
else
{
save();
return;
}
}
void Top_Menu::save()
{
if (window_->mode_flag_ == DRAW_2_CURVES && window_->input_menu_->choose_coloring_box_2_->currentIndex() == T_IMAGE
&& show_save_image_warning_)
{
QMessageBox message_box;
message_box.setWindowTitle("Save...");
message_box.setIcon(QMessageBox::Information);
QString message = QString("Saving the image is not supported.");
message_box.setText(message);
message_box.setInformativeText("You may need to set an image again when you load the file.");
message_box.setStandardButtons(QMessageBox::Ok);
message_box.exec();
show_save_image_warning_ = false;
}
if(last_loaded_file_.isEmpty())
{
save_as();
}
std::ofstream save_file(last_loaded_file_.toStdString().c_str(), std::ios::out | std::ios::trunc);
window_->configuration_->update_parameters();
window_->configuration_->write_in_file(save_file);
save_file.close();
return;
}
void Top_Menu::open()
{
//std::cout << "Entering in Top_Menu::load" << std::endl;
int load_success = 1;
last_loaded_file_ =
QFileDialog::getOpenFileName(this,
tr("Open file..."),
(last_loaded_file_.isEmpty()?" ":last_loaded_file_.toStdString().c_str()),
tr("CP files (*.cp);;All Files (*)"));
if(last_loaded_file_.isEmpty())
{
return;
}
std::ifstream load_file(last_loaded_file_.toStdString().c_str(), std::ios::in);
load_success = window_->configuration_->read_from_file(load_file);
if(load_success == 0)
{
window_->configuration_->restore_configuration();
}
else if(load_success == 1)
{
QMessageBox message_box_1;
message_box_1.setWindowTitle("Open file...");
message_box_1.setIcon(QMessageBox::Warning);
message_box_1.setText("The selected file is corrupted.");
message_box_1.setInformativeText("Do you want to retry?");
message_box_1.setStandardButtons(QMessageBox::No | QMessageBox::Yes);
message_box_1.setDefaultButton(QMessageBox::Yes);
int retour = message_box_1.exec();
switch(retour)
{
case QMessageBox::Yes :
open();
return;
break;
case QMessageBox::No :
return;
break;
default:
std::cout << "ERROR in Top_Menu::open: should never have reached here" << std::endl;
throw(QString("ERROR in Top_Menu::open: should never have reached here"));
}
}
else if(load_success == 2)
{
QMessageBox message_box_1;
message_box_1.setWindowTitle("Open file...");
message_box_1.setIcon(QMessageBox::Warning);
message_box_1.setText("The selected file is not a valid CP file.");
message_box_1.setInformativeText("Do you want to retry?");
message_box_1.setStandardButtons(QMessageBox::No | QMessageBox::Yes);
message_box_1.setDefaultButton(QMessageBox::Yes);
int retour = message_box_1.exec();
switch(retour)
{
case QMessageBox::Yes :
open();
return;
break;
case QMessageBox::No :
return;
break;
default:
std::cout << "ERROR in Top_Menu::open: should never have reached here" << std::endl;
throw(QString("ERROR in Top_Menu::open: should never have reached here"));
}
}
load_file.close();
return;
}
void Top_Menu::help()
{
Help_Browser::show_page();
return;
}
void Top_Menu::about()
{
QString text(tr("<p> Circle Packings v1.0" \
"<p> Created by Benjamin Beeker and Brice Loustau " \
"<br> (using Qt framework)" \
"<p> Based on an algorithm by C. R. Collins and K. Stephenson"));
QMessageBox::about(this, "About", text);
return;
}