MandelWidget.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  1. #include "MandelWidget.h"
  2. #include <cmath>
  3. using namespace mnd;
  4. #include <QOpenGLVertexArrayObject>
  5. Texture::Texture(const Bitmap<RGBColor>& bitmap) :
  6. context{ nullptr }
  7. {
  8. glGenTextures(1, &id);
  9. glBindTexture(GL_TEXTURE_2D, id);
  10. long lineLength = (bitmap.width * 3 + 3) & ~3;
  11. unsigned char* pixels = new unsigned char[lineLength * bitmap.height];
  12. for (int i = 0; i < bitmap.width; i++) {
  13. for (int j = 0; j < bitmap.height; j++) {
  14. int index = i * 3 + j * lineLength;
  15. RGBColor c = bitmap.get(i, j);
  16. pixels[index] = c.r;
  17. pixels[index + 1] = c.g;
  18. pixels[index + 2] = c.b;
  19. }
  20. }
  21. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, int(bitmap.width), int(bitmap.height), 0, GL_RGB, GL_UNSIGNED_BYTE, pixels);
  22. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  23. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  24. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  25. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  26. }
  27. Texture::Texture(const Bitmap<RGBColor>& bitmap, QOpenGLContext* context) :
  28. context{ context }
  29. {
  30. context->functions()->glGenTextures(1, &id);
  31. context->functions()->glBindTexture(GL_TEXTURE_2D, id);
  32. long lineLength = (bitmap.width * 3 + 3) & ~3;
  33. unsigned char* pixels = new unsigned char[lineLength * bitmap.height];
  34. for (int i = 0; i < bitmap.width; i++) {
  35. for (int j = 0; j < bitmap.height; j++) {
  36. int index = i * 3 + j * lineLength;
  37. RGBColor c = bitmap.get(i, j);
  38. pixels[index] = c.r;
  39. pixels[index + 1] = c.g;
  40. pixels[index + 2] = c.b;
  41. }
  42. }
  43. context->functions()->glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, int(bitmap.width), int(bitmap.height), 0, GL_RGB, GL_UNSIGNED_BYTE, pixels);
  44. context->functions()->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  45. context->functions()->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  46. context->functions()->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  47. context->functions()->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  48. }
  49. Texture::~Texture(void)
  50. {
  51. if (id != 0)
  52. glDeleteTextures(1, &id);
  53. }
  54. Texture::Texture(Texture&& other) :
  55. id{ other.id },
  56. context{ other.context }
  57. {
  58. other.id = 0;
  59. }
  60. Texture& Texture::operator=(Texture&& other)
  61. {
  62. this->id = other.id;
  63. this->context = other.context;
  64. other.id = 0;
  65. return *this;
  66. }
  67. void Texture::bind(void) const
  68. {
  69. glBindTexture(GL_TEXTURE_2D, id);
  70. }
  71. void Texture::drawRect(float x, float y, float width, float height)
  72. {
  73. glColor3ub(255, 255, 255);
  74. glEnable(GL_TEXTURE_2D);
  75. bind();
  76. glBegin(GL_TRIANGLE_STRIP);
  77. glTexCoord2f(0, 0);
  78. glVertex2f(x, y);
  79. glTexCoord2f(1, 0);
  80. glVertex2f(x + width, y);
  81. glTexCoord2f(0, 1);
  82. glVertex2f(x, y + height);
  83. glTexCoord2f(1, 1);
  84. glVertex2f(x + width, y + height);
  85. glEnd();
  86. glDisable(GL_TEXTURE_2D);
  87. }
  88. std::pair<int, int> TexGrid::getCellIndices(double x, double y)
  89. {
  90. return { ::floor(x / dpp / MandelV::chunkSize), ::floor(y / dpp / MandelV::chunkSize) };
  91. }
  92. std::pair<double, double> TexGrid::getPositions(int x, int y)
  93. {
  94. return { x * dpp * MandelV::chunkSize, y * dpp * MandelV::chunkSize };
  95. }
  96. Texture* TexGrid::getCell(int i, int j)
  97. {
  98. auto cIt = cells.find({i, j});
  99. if (cIt != cells.end()) {
  100. return cIt->second.get();
  101. }
  102. else {
  103. return nullptr;
  104. }
  105. }
  106. void TexGrid::setCell(int i, int j, std::unique_ptr<Texture> tex)
  107. {
  108. cells[{i, j}] = std::move(tex);
  109. }
  110. void TexGrid::clearCells(void)
  111. {
  112. cells.clear();
  113. }
  114. void Job::run(void)
  115. {
  116. auto [absX, absY] = grid->getPositions(i, j);
  117. double gw = grid->dpp * MandelV::chunkSize;
  118. Bitmap<float> f(MandelV::chunkSize, MandelV::chunkSize);
  119. mnd::MandelInfo mi;
  120. mi.view.x = absX;
  121. mi.view.y = absY;
  122. mi.view.width = mi.view.height = gw;
  123. mi.bWidth = mi.bHeight = MandelV::chunkSize;
  124. mi.maxIter = 500;
  125. mndContext.getDefaultGenerator().generate(mi, f.pixels.get());
  126. Bitmap<RGBColor>* rgb = new Bitmap<RGBColor>(f.map<RGBColor>([] (float f) {
  127. return RGBColor{ uint8_t(f / 2), uint8_t(f / 2), uint8_t(f / 2) };
  128. }));
  129. emit done(level, i, j, rgb);
  130. }
  131. void Calcer::calc(TexGrid& grid, int level, int i, int j)
  132. {
  133. if (jobs.find({ level, i, j }) == jobs.end()) {
  134. Job* job = new Job(mndContext, &grid, level, i, j);
  135. connect(job, &Job::done, this, &Calcer::redirect);
  136. jobs.insert({ level, i, j });
  137. //jobs.push_back(std::move(job));
  138. threadPool->start(job);
  139. }
  140. }
  141. void Calcer::redirect(int level, int i, int j, Bitmap<RGBColor>* bmp)
  142. {
  143. jobs.erase({ level, i, j });
  144. emit done(level, i, j, bmp);
  145. }
  146. MandelV::MandelV(mnd::MandelContext& mndContext) :
  147. mndContext{ mndContext },
  148. calcThread{ std::make_unique<Calcer>(mndContext) }
  149. {
  150. Bitmap<RGBColor> emp(8, 8);
  151. for(auto i = 0; i < emp.width; i++) {
  152. for(auto j = 0; j < emp.height; j++) {
  153. if((i + j) & 0x1) { // if i+j is odd
  154. emp.get(i, j) = RGBColor{ 255, 255, 255 };
  155. }
  156. else {
  157. emp.get(i, j) = RGBColor{ 120, 120, 120 };
  158. }
  159. }
  160. }
  161. empty = std::make_unique<Texture>(emp);
  162. connect(calcThread.get(), &Calcer::done, this, &MandelV::cellReady);
  163. }
  164. int MandelV::getLevel(double dpp) {
  165. return -int(::log2(dpp / chunkSize));
  166. }
  167. double MandelV::getDpp(int level)
  168. {
  169. return ::pow(2, -level) * chunkSize;
  170. }
  171. TexGrid& MandelV::getGrid(int level)
  172. {
  173. auto it = levels.find(level);
  174. if (it != levels.end())
  175. return it->second;
  176. else {
  177. levels.insert({ level, TexGrid(getDpp(level)) });
  178. return levels[level];
  179. }
  180. }
  181. void MandelV::garbageCollect(int level)
  182. {
  183. for(auto& [l, grid] : levels) {
  184. int dist = ::abs(l - level);
  185. if (dist > 20) {
  186. grid.clearCells();
  187. }
  188. else if (dist > 10) {
  189. if (grid.countAllocatedCells() > 20)
  190. grid.clearCells();
  191. }
  192. else if (dist > 3) {
  193. if (grid.countAllocatedCells() > 80)
  194. grid.clearCells();
  195. }
  196. else if (dist > 0) {
  197. if (grid.countAllocatedCells() > 150)
  198. grid.clearCells();
  199. }
  200. else {
  201. if (grid.countAllocatedCells() > 250)
  202. grid.clearCells();
  203. }
  204. }
  205. }
  206. void MandelV::paint(const mnd::MandelViewport& mvp)
  207. {
  208. double dpp = mvp.width / width;
  209. int level = getLevel(dpp);
  210. garbageCollect(level);
  211. auto& grid = getGrid(level);
  212. double gw = getDpp(level) * chunkSize;
  213. double w = width * gw / mvp.width;
  214. //double h = height * gw / mvp.height;
  215. auto [left, top] = grid.getCellIndices(mvp.x, mvp.y);
  216. auto [right, bottom] = grid.getCellIndices(mvp.right(), mvp.bottom());
  217. auto [realXLeft, realYTop] = grid.getPositions(left, top);
  218. realXLeft = (realXLeft - mvp.x) * width / mvp.width;
  219. realYTop = (realYTop - mvp.y) * height / mvp.height;
  220. for(int i = left; i <= right; i++) {
  221. for(int j = top; j <= bottom; j++) {
  222. double x = realXLeft + (i - left) * w;
  223. double y = realYTop + (j - top) * w;
  224. Texture* t = grid.getCell(i, j);
  225. if (t != nullptr) {
  226. t->drawRect(x, y, w, w);
  227. /*glBegin(GL_LINE_LOOP);
  228. glVertex2f(x, y);
  229. glVertex2f(x + w, y);
  230. glVertex2f(x + w, y + w);
  231. glVertex2f(x, y + w);
  232. glEnd();*/
  233. }
  234. else {
  235. calcThread->calc(grid, level, i, j);
  236. this->empty->drawRect(x, y, w, w);
  237. }
  238. }
  239. }
  240. }
  241. void MandelV::cellReady(int level, int i, int j, Bitmap<RGBColor>* bmp)
  242. {
  243. this->getGrid(level).setCell(i, j, std::make_unique<Texture>(*bmp));
  244. delete bmp;
  245. emit redrawRequested();
  246. }
  247. MandelView::MandelView(mnd::Generator& generator, Gradient &gradient, MandelWidget* mWidget) :
  248. generator{ &generator },
  249. gradient{ gradient },
  250. mWidget{ mWidget }
  251. //context{ new QOpenGLContext(this) }
  252. {
  253. //context->setShareContext(mWidget->context()->contextHandle());
  254. hasToCalc.store(false);
  255. finish.store(false);
  256. }
  257. MandelView::~MandelView(void)
  258. {
  259. finish.store(true);
  260. condVar.notify_one();
  261. //calcThread.wait(100);
  262. calcThread.wait(100);
  263. calcThread.terminate();
  264. }
  265. void MandelView::setGenerator(mnd::Generator& value)
  266. {
  267. generator = &value;
  268. }
  269. void MandelView::start(void)
  270. {
  271. this->moveToThread(&calcThread);
  272. connect(&calcThread, SIGNAL(started()), this, SLOT(loop()));
  273. calcThread.start();
  274. }
  275. void MandelView::loop(void)
  276. {
  277. printf("thread!\n"); fflush(stdout);
  278. //QGLWidget* hiddenWidget = new QGLWidget(nullptr, mWidget);
  279. //hiddenWidget->setVisible(false);
  280. //hiddenWidget->context()->contextHandle()->moveToThread(&calcThread);
  281. //QOpenGLContext* context = hiddenWidget->context()->contextHandle();
  282. //context->setShareContext(mWidget->context()->contextHandle());
  283. //context->create();
  284. //printf("sharing: %d\n", QOpenGLContext::areSharing(hiddenWidget->context()->contextHandle(), mWidget->context()->contextHandle()));
  285. //fflush(stdout);
  286. //std::this_thread::sleep_for(std::chrono::milliseconds(3000));
  287. std::unique_lock<std::mutex> lock(mut);
  288. while(true) {
  289. printf("calcing!\n"); fflush(stdout);
  290. if (finish.load()) {
  291. break;
  292. }
  293. if (hasToCalc.exchange(false)) {
  294. const MandelInfo& mi = toCalc.load();
  295. auto fmap = Bitmap<float>(mi.bWidth, mi.bHeight);
  296. generator->generate(mi, fmap.pixels.get());
  297. auto* bitmap = new Bitmap<RGBColor>(fmap.map<RGBColor>([&mi, this](float i) {
  298. return i >= mi.maxIter ? RGBColor{ 0, 0, 0 } : gradient.get(i);
  299. }));
  300. /*return i >= mi.maxIter ?
  301. RGBColor{ 0,0,0 } :
  302. RGBColor{ uint8_t(cos(i * 0.015f) * 127 + 127),
  303. uint8_t(sin(i * 0.01f) * 127 + 127),
  304. uint8_t(i) }; }));//uint8_t(::sin(i * 0.01f) * 100 + 100), uint8_t(i) }; });
  305. */
  306. //hiddenWidget->makeCurrent();
  307. //Texture* tex = new Texture(bitmap);
  308. //hiddenWidget->doneCurrent();
  309. //Texture* tex = 0;
  310. emit updated(bitmap);
  311. }
  312. printf("finished calcing!\n"); fflush(stdout);
  313. condVar.wait(lock);
  314. printf("waking!\n"); fflush(stdout);
  315. }
  316. }
  317. void MandelView::adaptViewport(const MandelInfo mi)
  318. {
  319. //bmp->get(0, 0) = RGBColor{ 10, uint8_t(sin(1 / vp.width) * 127 + 127), 10 };
  320. /*printf("adapted\n");
  321. if (calc.valid()) {
  322. auto status = calc.wait_for(std::chrono::milliseconds(0));
  323. if (status == std::future_status::deferred) {
  324. printf("deferred\n");
  325. } else if (status == std::future_status::timeout) {
  326. printf("timeout\n");
  327. } else if (status == std::future_status::ready) {
  328. printf("ready!\n");
  329. }
  330. }*/
  331. /*if (!calc.valid() || calc.wait_for(std::chrono::milliseconds(0)) == std::future_status::ready) {
  332. toCalc = mi;
  333. hasToCalc = true;
  334. calc = std::async([this, mi] () {
  335. QGLWidget* hiddenWidget = new QGLWidget(nullptr, (QGLWidget*) mWidget);
  336. QOpenGLContext* context = hiddenWidget->context()->contextHandle();
  337. hiddenWidget->makeCurrent();
  338. //context->setShareContext(mWidget->context()->contextHandle());
  339. //context->create();
  340. printf("sharing: %d\n", QOpenGLContext::areSharing(context, mWidget->context()->contextHandle()));
  341. fflush(stdout);
  342. //std::this_thread::sleep_for(std::chrono::milliseconds(1000));
  343. do {
  344. auto fmap = Bitmap<float>(mi.bWidth, mi.bHeight);
  345. generator->generate(mi, fmap.pixels.get());
  346. auto bitmap = fmap.map<RGBColor>([&mi](float i) { return i > mi.maxIter ?
  347. RGBColor{ 0,0,0 } :
  348. RGBColor{ uint8_t(cos(i * 0.015f) * 127 + 127),
  349. uint8_t(sin(i * 0.01f) * 127 + 127),
  350. uint8_t(i) }; });//uint8_t(::sin(i * 0.01f) * 100 + 100), uint8_t(i) }; });
  351. Texture* tex = new Texture(bitmap, context);
  352. //Texture* tex = 0;
  353. emit updated(tex);
  354. } while(hasToCalc.exchange(false));
  355. });
  356. }
  357. else {*/
  358. //std::unique_lock<std::mutex> lock(mut, std::try_to_lock);
  359. toCalc = mi;
  360. hasToCalc.exchange(true);
  361. condVar.notify_one();
  362. //}
  363. }
  364. MandelWidget::MandelWidget(mnd::MandelContext& ctxt, QWidget* parent) :
  365. QOpenGLWidget{ parent },
  366. mndContext{ ctxt },
  367. mv{ ctxt.getDefaultGenerator(), gradient, this }
  368. {
  369. this->setContentsMargins(0, 0, 0, 0);
  370. this->setSizePolicy(QSizePolicy::Expanding,
  371. QSizePolicy::Expanding);
  372. QObject::connect(&mv, &MandelView::updated, this, &MandelWidget::viewUpdated, Qt::AutoConnection);
  373. QObject::connect(this, &MandelWidget::needsUpdate, &mv, &MandelView::adaptViewport, Qt::DirectConnection);
  374. /*if (!ctxt.getDevices().empty()) {
  375. if (auto* gen = ctxt.getDevices()[0].getGeneratorDouble(); gen) {
  376. mv.setGenerator(*gen);
  377. }
  378. }*/
  379. }
  380. MandelWidget::~MandelWidget()
  381. {
  382. }
  383. void MandelWidget::initializeGL(void)
  384. {
  385. this->context()->functions()->glClearColor(0, 0, 0, 0);
  386. this->context()->makeCurrent(nullptr);
  387. glDisable(GL_DEPTH_TEST);
  388. // looks not even better
  389. //glDisable(GL_FRAMEBUFFER_SRGB);
  390. //glShadeModel(GL_SMOOTH);
  391. /*CpuGenerator<double> cpg;
  392. MandelInfo mi;
  393. mi.bWidth = this->width();//ql.geometry().width();
  394. mi.bHeight = this->height(); //ql.geometry().height();
  395. mi.maxIter = 250;
  396. mi.view = viewport;
  397. auto bitmap = cpg.generate(mi);*/
  398. Bitmap<RGBColor> bitmap(1, 1);
  399. bitmap.get(0, 0) = RGBColor{50, 50, 50};
  400. v = nullptr;
  401. tex = std::make_unique<Texture>(bitmap, context());
  402. mv.start();
  403. requestRecalc();
  404. }
  405. void MandelWidget::paintGL(void)
  406. {
  407. if (v == nullptr) {
  408. v = std::make_unique<MandelV>(mndContext);
  409. QObject::connect(v.get(), &MandelV::redrawRequested, this, static_cast<void(QOpenGLWidget::*)(void)>(&QOpenGLWidget::update));
  410. }
  411. /*if (!initialized) {
  412. emit needsUpdate(viewport);
  413. initialized = true;
  414. }*/
  415. int width = this->width();
  416. int height = this->height();
  417. v->width = width;
  418. v->height = height;
  419. //v = std::make_unique<MandelV>(context());
  420. /*CpuGenerator<double> cpg;
  421. ClGenerator clg;
  422. MandelGenerator& mg = cpg;
  423. MandelInfo mi;
  424. mi.bWidth = width;
  425. mi.bHeight = height;
  426. mi.maxIter = 5000;
  427. mi.view = viewport;*/
  428. //auto bitmap = mg.generate(mi);
  429. /*Bitmap<RGBColor> bitmap(1000, 1000);
  430. for (int i = 0; i < 1000 * 1000; i++)
  431. bitmap.pixels[i] = RGBColor{5, uint8_t((i % 1000) ^ (i / 1000)), 50};
  432. tex = std::make_unique<Texture>(bitmap);*/
  433. glViewport(0, 0, width, height);
  434. glMatrixMode(GL_PROJECTION);
  435. glLoadIdentity();
  436. #ifdef QT_OPENGL_ES_1
  437. glOrthof(0, width, height, 0, -1.0, 1.0);
  438. #else
  439. glOrtho(0, width, height, 0, -1.0, 1.0);
  440. #endif
  441. glMatrixMode(GL_MODELVIEW);
  442. glClear(GL_COLOR_BUFFER_BIT);
  443. glLoadIdentity();
  444. //tex->drawRect(0, 0, width, height);
  445. //v->empty = std::move(*tex)
  446. //v->empty.bind();
  447. v->paint(this->viewport);
  448. //*tex = std::move(v->empty);
  449. if (rubberbandDragging)
  450. drawRubberband();
  451. printf("painted GL\n");
  452. }
  453. void MandelWidget::drawRubberband(void)
  454. {
  455. glColor3ub(10, 200, 10);
  456. glBegin(GL_LINE_LOOP);
  457. glVertex2d(rubberband.x(), rubberband.y());
  458. glVertex2d(rubberband.right(), rubberband.y());
  459. glVertex2d(rubberband.right(), rubberband.bottom());
  460. glVertex2d(rubberband.x(), rubberband.bottom());
  461. glEnd();
  462. }
  463. void MandelWidget::zoom(float scale)
  464. {
  465. viewport.zoomCenter(scale);
  466. requestRecalc();
  467. }
  468. void MandelWidget::setMaxIterations(int maxIter)
  469. {
  470. this->maxIterations = maxIter;
  471. requestRecalc();
  472. }
  473. void MandelWidget::requestRecalc()
  474. {
  475. //emit needsUpdate(MandelInfo{ viewport, this->width(), this->height(), maxIterations });
  476. this->update();
  477. }
  478. void MandelWidget::resizeGL(int width, int height)
  479. {
  480. //glViewport(0, 0, (GLint) width, (GLint) height);
  481. this->update();
  482. }
  483. /*void MandelWidget::redraw(void)
  484. {
  485. /*CpuGenerator<double> cpg;
  486. MandelInfo mi;
  487. mi.bWidth = this->geometry().width();//ql.geometry().width();
  488. mi.bHeight = this->geometry().height(); //ql.geometry().height();
  489. mi.maxIter = 250;
  490. mi.view = viewport;*/
  491. //update();
  492. //emit needsUpdate(viewport);
  493. //auto bitmap = cpg.generate(mi).map<uint32_t>([](RGBColor rgb) { return 255 << 24 | rgb.b << 16 | rgb.g << 8 | rgb.r; });
  494. //}
  495. void MandelWidget::resizeEvent(QResizeEvent* re)
  496. {
  497. QOpenGLWidget::resizeEvent(re);
  498. double aspect = double(geometry().width()) / geometry().height();
  499. //if (viewport.width > viewport.height * aspect)
  500. viewport.height = (viewport.width / aspect);
  501. //else
  502. // viewport.width = (viewport.height * aspect);
  503. if (v.get() != nullptr) {
  504. v->width = this->width();
  505. v->height = this->height();
  506. }
  507. printf("resized\n");
  508. requestRecalc();
  509. //redraw();
  510. }
  511. void MandelWidget::mousePressEvent(QMouseEvent* me)
  512. {
  513. rubberband.setCoords(me->x(), me->y(), 0, 0);
  514. rubberbandDragging = true;
  515. }
  516. void MandelWidget::mouseMoveEvent(QMouseEvent* me)
  517. {
  518. QRectF& rect = rubberband;
  519. float aspect = float(geometry().width()) / geometry().height();
  520. rect.setBottomRight(QPoint(me->x(), me->y()));
  521. if (rect.width() > rect.height() * aspect)
  522. rect.setHeight(rect.width() / aspect);
  523. else
  524. rect.setWidth(rect.height() * aspect);
  525. if (rubberbandDragging)
  526. emit repaint();
  527. }
  528. void MandelWidget::mouseReleaseEvent(QMouseEvent* me)
  529. {
  530. QRect rect = rubberband.toRect();
  531. QRect full = this->geometry();
  532. viewport.x += double(rect.left()) * viewport.width / full.width();
  533. viewport.y += double(rect.top()) * viewport.height / full.height();
  534. viewport.width *= double(rect.width()) / full.width();
  535. viewport.height *= double(rect.height()) / full.height();
  536. viewport.normalize();
  537. rubberbandDragging = false;
  538. requestRecalc();
  539. }
  540. void MandelWidget::viewUpdated(Bitmap<RGBColor>* bitmap)
  541. {
  542. if (bitmap != nullptr) {
  543. tex = std::make_unique<Texture>(*bitmap);
  544. delete bitmap;
  545. emit repaint();
  546. }
  547. }