2 // bigintmandelwidget.cpp
9 #include <QtConcurrent/QtConcurrent>
11 #include "bigintmandelwidget.h"
12 #include "mandellabel.h"
15 BigintMandelWidget::BigintMandelWidget(QWidget
*parent
)
17 fw(new QFutureWatcher
<MandelResultCell
>(this)),
18 settings(128, QSize(502, 334)),
19 menu_bar(new MenuBar(this)),
20 scroll_area(new QScrollArea(this)),
21 mandel_label(new MandelLabel(this)),
22 status_bar(new QStatusBar(this)),
23 settings_widget(new SettingsWidget(this))
25 scroll_area
->setWidget(mandel_label
);
30 &BigintMandelWidget::settings_widget_accepted
34 &QFutureWatcher
<MandelResultCell
>::resultReadyAt
,
36 &BigintMandelWidget::finished_cell
40 &QFutureWatcher
<MandelResultCell
>::finished
,
42 &BigintMandelWidget::finished
44 setLayout(new QVBoxLayout());
45 layout()->addWidget(menu_bar
);
46 layout()->addWidget(scroll_area
);
47 status_bar
->setSizeGripEnabled(false);
48 layout()->addWidget(status_bar
);
49 mandel_label
->resize(settings
.get_params().get_size());
53 BigintMandelWidget::~BigintMandelWidget() {
55 fw
->waitForFinished();
58 void BigintMandelWidget::finished_cell(int num
) {
59 QSize size
= settings
.get_params().get_size();
60 int y
= num
/ size
.width() + 1;
61 mandel_label
->set_draw_progress(y
< size
.height() - 1 ? y
+ 1 : -1);
62 settings
.finished_cell(num
, fw
->resultAt(num
));
66 void BigintMandelWidget::finished() {
67 mandel_label
->set_draw_progress(-1);
68 settings_widget
->set_finished(true);
72 void BigintMandelWidget::update_status_bar() {
74 static int prev_num_threads
= -1;
79 num_threads
= QThreadPool::globalInstance()->activeThreadCount();
80 if (num_threads
== prev_num_threads
)
82 ss
.setString(new QString());
83 if (num_threads
!= -1)
84 ss
<< "Calculating with " << num_threads
<< " threads ...";
86 ss
<< "Click the rendering to zoom.";
87 status_bar
->showMessage(*ss
.string());
88 prev_num_threads
= num_threads
;
91 void BigintMandelWidget::start() {
95 [](const MandelCell
&cell
){ return cell
.iterate(); }
98 menu_bar
->set_zoom_factor(-1);
102 void BigintMandelWidget::mousePressEvent(QMouseEvent
*event
) {
103 QSize
size(settings
.get_params().get_size());
104 QPoint
pos(event
->pos());
106 int zoom_factor
= menu_bar
->get_zoom_factor();
107 for (w
= mandel_label
; w
!= this; w
= w
->parentWidget())
108 pos
= w
->mapFromParent(pos
);
109 if (event
->button() != Qt::MouseButton::LeftButton
112 || pos
.x() >= size
.width()
114 || pos
.y() >= size
.height()
115 || zoom_factor
== -1)
117 settings
.zoom(get_ideal_size(), zoom_factor
, pos
);
121 static inline QString
get_save_file_name(
122 QWidget
*parent
= nullptr,
123 const QString
&caption
= QString(),
124 const QString
&dir
= QString(),
125 const QString
&filter_title
= QString(),
126 const QStringList
&ext_list
= QStringList
{},
127 const QString default_ext
= QString()
129 QTextStream
ss(new QString());
130 QStringList::const_iterator ext_list_iter
;
132 ss
<< filter_title
<< " (" << ext_list
.join(" ") << ")";
133 file_name
= QFileDialog::getSaveFileName(
134 parent
, caption
, dir
, *ss
.string()
136 if (file_name
.isEmpty())
138 ss
.string()->clear();
141 ext_list_iter
= ext_list
.constBegin();
142 ext_list_iter
!= ext_list
.constEnd();
145 if (file_name
.endsWith(*ext_list_iter
))
147 if (ext_list_iter
== ext_list
.constEnd())
152 void BigintMandelWidget::export_img() {
153 QString file_name
= get_save_file_name(
158 QStringList
{".png", ".xpm", ".jpg"},
161 if (!file_name
.isEmpty())
162 settings
.save_img(file_name
);
165 void BigintMandelWidget::exec_settings_widget() {
166 settings_widget
->update_fields(settings
.get_params(), fw
->isFinished());
167 settings_widget
->exec();
170 void BigintMandelWidget::reset() {
172 fw
->waitForFinished();
173 settings
.reset(128, get_ideal_size());
177 void BigintMandelWidget::settings_widget_accepted() {
178 settings
.set_max_iter(settings_widget
->get_max_iter().toULongLong());
182 void BigintMandelWidget::load_data() {
186 QString file_name
= QFileDialog::getOpenFileName(
187 this, "Load Data", "", "BigintMandel Data (*.json *.json.qcompress)"
189 if (file_name
.isEmpty())
191 file
.setFileName(file_name
);
192 if (!file
.open(QIODevice::ReadOnly
)) {
193 qWarning("Couldn't open save file.");
196 qba
= file
.readAll();
198 if (file_name
.endsWith(".qcompress"))
199 qba
= qUncompress(qba
);
200 settings
.from_json(QJsonDocument::fromJson(qba
).object());
204 void BigintMandelWidget::save_data() {
207 QString file_name
= get_save_file_name(
212 QStringList
{".json", ".json.qcompress"},
215 if (file_name
.isEmpty())
217 qba
= QJsonDocument(settings
.to_json()).toJson();
218 if (file_name
.endsWith(".qcompress"))
219 qba
= qCompress(qba
, 9);
220 file
.setFileName(file_name
);
221 if (!file
.open(QIODevice::WriteOnly
)) {
222 qWarning("Couldn't open save file.");
229 void BigintMandelWidget::update() {