Переглянути джерело

using QPainters for drawing rects and points

Nicolas Winkler 5 роки тому
батько
коміт
99437362d0
5 змінених файлів з 207 додано та 229 видалено
  1. 164 143
      Almond.ui
  2. 1 1
      CMakeLists.txt
  3. 37 8
      MandelWidget.cpp
  4. 3 1
      MandelWidget.h
  5. 2 76
      main.cpp

+ 164 - 143
Almond.ui

@@ -9,8 +9,8 @@
    <rect>
     <x>0</x>
     <y>0</y>
-    <width>950</width>
-    <height>744</height>
+    <width>1202</width>
+    <height>829</height>
    </rect>
   </property>
   <property name="windowTitle">
@@ -44,124 +44,146 @@
      <item>
       <layout class="QVBoxLayout" name="verticalLayout_right">
        <item>
-        <layout class="QFormLayout" name="formLayout">
-         <item row="1" column="0">
-          <widget class="QLabel" name="label">
-           <property name="text">
-            <string>max. iterations</string>
-           </property>
-          </widget>
-         </item>
-         <item row="1" column="1">
-          <widget class="QLineEdit" name="maxIterations">
-           <property name="minimumSize">
-            <size>
-             <width>70</width>
-             <height>0</height>
-            </size>
-           </property>
-           <property name="text">
-            <string>500</string>
-           </property>
-           <property name="maxLength">
-            <number>32</number>
-           </property>
-          </widget>
-         </item>
-         <item row="0" column="0" colspan="2">
-          <widget class="QPushButton" name="chooseGradient">
-           <property name="sizePolicy">
-            <sizepolicy hsizetype="Minimum" vsizetype="Fixed">
-             <horstretch>0</horstretch>
-             <verstretch>0</verstretch>
-            </sizepolicy>
-           </property>
-           <property name="text">
-            <string>Choose Gradient</string>
-           </property>
-          </widget>
-         </item>
-         <item row="2" column="0" colspan="2">
-          <widget class="QCheckBox" name="smooth">
-           <property name="sizePolicy">
-            <sizepolicy hsizetype="Minimum" vsizetype="Expanding">
-             <horstretch>0</horstretch>
-             <verstretch>0</verstretch>
-            </sizepolicy>
-           </property>
-           <property name="text">
-            <string>smooth coloring</string>
-           </property>
-           <property name="checked">
-            <bool>true</bool>
-           </property>
-          </widget>
-         </item>
-         <item row="3" column="0" colspan="2">
-          <widget class="QCheckBox" name="displayInfo">
-           <property name="sizePolicy">
-            <sizepolicy hsizetype="Minimum" vsizetype="Expanding">
-             <horstretch>0</horstretch>
-             <verstretch>0</verstretch>
-            </sizepolicy>
-           </property>
-           <property name="text">
-            <string>display scale</string>
-           </property>
-          </widget>
-         </item>
-        </layout>
+        <widget class="QGroupBox" name="groupBox_3">
+         <property name="title">
+          <string>Display/Calculation Options</string>
+         </property>
+         <layout class="QVBoxLayout" name="verticalLayout_3">
+          <item>
+           <layout class="QFormLayout" name="formLayout">
+            <item row="1" column="0">
+             <widget class="QLabel" name="label">
+              <property name="text">
+               <string>Max. Iterations</string>
+              </property>
+             </widget>
+            </item>
+            <item row="2" column="0" colspan="2">
+             <widget class="QCheckBox" name="smooth">
+              <property name="sizePolicy">
+               <sizepolicy hsizetype="Minimum" vsizetype="Expanding">
+                <horstretch>0</horstretch>
+                <verstretch>0</verstretch>
+               </sizepolicy>
+              </property>
+              <property name="text">
+               <string>smooth coloring</string>
+              </property>
+              <property name="checked">
+               <bool>true</bool>
+              </property>
+             </widget>
+            </item>
+            <item row="3" column="0" colspan="2">
+             <widget class="QCheckBox" name="displayInfo">
+              <property name="sizePolicy">
+               <sizepolicy hsizetype="Minimum" vsizetype="Expanding">
+                <horstretch>0</horstretch>
+                <verstretch>0</verstretch>
+               </sizepolicy>
+              </property>
+              <property name="text">
+               <string>display scale</string>
+              </property>
+             </widget>
+            </item>
+            <item row="4" column="0" colspan="2">
+             <widget class="QPushButton" name="chooseGenerator">
+              <property name="text">
+               <string>Select Generators</string>
+              </property>
+             </widget>
+            </item>
+            <item row="1" column="1">
+             <widget class="QLineEdit" name="maxIterations">
+              <property name="minimumSize">
+               <size>
+                <width>70</width>
+                <height>0</height>
+               </size>
+              </property>
+              <property name="text">
+               <string>500</string>
+              </property>
+              <property name="maxLength">
+               <number>32</number>
+              </property>
+             </widget>
+            </item>
+            <item row="0" column="0" colspan="2">
+             <widget class="QPushButton" name="chooseGradient">
+              <property name="sizePolicy">
+               <sizepolicy hsizetype="Minimum" vsizetype="Fixed">
+                <horstretch>0</horstretch>
+                <verstretch>0</verstretch>
+               </sizepolicy>
+              </property>
+              <property name="text">
+               <string>Choose Gradient</string>
+              </property>
+             </widget>
+            </item>
+           </layout>
+          </item>
+         </layout>
+        </widget>
        </item>
        <item>
-        <spacer name="verticalSpacer">
+        <spacer name="verticalSpacer_3">
          <property name="orientation">
           <enum>Qt::Vertical</enum>
          </property>
-         <property name="sizeType">
-          <enum>QSizePolicy::Expanding</enum>
-         </property>
          <property name="sizeHint" stdset="0">
           <size>
-           <width>20</width>
-           <height>40</height>
+           <width>0</width>
+           <height>0</height>
           </size>
          </property>
         </spacer>
        </item>
        <item>
         <layout class="QGridLayout" name="gridLayout">
-         <item row="0" column="1">
-          <widget class="QPushButton" name="zoom_out">
-           <property name="text">
-            <string>Zoom Out</string>
-           </property>
-          </widget>
-         </item>
          <item row="0" column="0">
-          <widget class="QPushButton" name="zoom_in">
-           <property name="text">
-            <string>Zoom In</string>
-           </property>
-          </widget>
-         </item>
-         <item row="1" column="0" colspan="2">
-          <widget class="QPushButton" name="resetZoom">
-           <property name="text">
-            <string>Reset Zoom</string>
+          <widget class="QGroupBox" name="groupBox_2">
+           <property name="title">
+            <string>Zoom</string>
            </property>
+           <layout class="QGridLayout" name="gridLayout_2">
+            <item row="1" column="0">
+             <widget class="QPushButton" name="zoom_in">
+              <property name="text">
+               <string>Zoom In</string>
+              </property>
+             </widget>
+            </item>
+            <item row="1" column="1">
+             <widget class="QPushButton" name="zoom_out">
+              <property name="text">
+               <string>Zoom Out</string>
+              </property>
+             </widget>
+            </item>
+            <item row="0" column="0" colspan="2">
+             <widget class="QPushButton" name="resetZoom">
+              <property name="text">
+               <string>Reset Zoom</string>
+              </property>
+             </widget>
+            </item>
+           </layout>
           </widget>
          </item>
         </layout>
        </item>
        <item>
-        <spacer name="verticalSpacer_3">
+        <spacer name="verticalSpacer_2">
          <property name="orientation">
           <enum>Qt::Vertical</enum>
          </property>
          <property name="sizeHint" stdset="0">
           <size>
-           <width>20</width>
-           <height>40</height>
+           <width>0</width>
+           <height>0</height>
           </size>
          </property>
         </spacer>
@@ -211,65 +233,64 @@
         </widget>
        </item>
        <item>
-        <spacer name="verticalSpacer_2">
+        <spacer name="verticalSpacer">
          <property name="orientation">
           <enum>Qt::Vertical</enum>
          </property>
-         <property name="sizeType">
-          <enum>QSizePolicy::MinimumExpanding</enum>
-         </property>
          <property name="sizeHint" stdset="0">
           <size>
-           <width>20</width>
-           <height>40</height>
+           <width>0</width>
+           <height>0</height>
           </size>
          </property>
         </spacer>
        </item>
        <item>
-        <widget class="QPushButton" name="chooseGenerator">
-         <property name="text">
-          <string>Select Generators</string>
-         </property>
-        </widget>
-       </item>
-       <item>
-        <widget class="QPushButton" name="exportVideo">
-         <property name="text">
-          <string>Export Video</string>
-         </property>
-        </widget>
-       </item>
-       <item>
-        <widget class="QPushButton" name="exportImage">
-         <property name="text">
-          <string>Export Image</string>
-         </property>
-        </widget>
-       </item>
-       <item>
-        <widget class="QProgressBar" name="backgroundProgress">
-         <property name="enabled">
-          <bool>true</bool>
-         </property>
-         <property name="sizePolicy">
-          <sizepolicy hsizetype="Expanding" vsizetype="Preferred">
-           <horstretch>0</horstretch>
-           <verstretch>0</verstretch>
-          </sizepolicy>
-         </property>
-         <property name="maximum">
-          <number>1</number>
-         </property>
-         <property name="value">
-          <number>0</number>
-         </property>
-         <property name="alignment">
-          <set>Qt::AlignCenter</set>
-         </property>
-         <property name="format">
-          <string>Background Tasks</string>
+        <widget class="QGroupBox" name="export_box">
+         <property name="title">
+          <string>Export</string>
          </property>
+         <layout class="QVBoxLayout" name="verticalLayout_2">
+          <item>
+           <widget class="QPushButton" name="exportVideo">
+            <property name="text">
+             <string>Export Video</string>
+            </property>
+           </widget>
+          </item>
+          <item>
+           <widget class="QPushButton" name="exportImage">
+            <property name="text">
+             <string>Export Image</string>
+            </property>
+           </widget>
+          </item>
+          <item>
+           <widget class="QProgressBar" name="backgroundProgress">
+            <property name="enabled">
+             <bool>true</bool>
+            </property>
+            <property name="sizePolicy">
+             <sizepolicy hsizetype="Expanding" vsizetype="Preferred">
+              <horstretch>0</horstretch>
+              <verstretch>0</verstretch>
+             </sizepolicy>
+            </property>
+            <property name="maximum">
+             <number>1</number>
+            </property>
+            <property name="value">
+             <number>0</number>
+            </property>
+            <property name="alignment">
+             <set>Qt::AlignCenter</set>
+            </property>
+            <property name="format">
+             <string>Background Tasks</string>
+            </property>
+           </widget>
+          </item>
+         </layout>
         </widget>
        </item>
       </layout>

+ 1 - 1
CMakeLists.txt

@@ -75,7 +75,7 @@ ELSEIF (UNIX AND NOT APPLE)
     set(CPACK_SOURCE_GENERATOR "DEB")
     set(CPACK_COMPONENTS_ALL Almond)
     set(CPACK_DEBIAN_PACKAGE_MAINTAINER "Nicolas Winkler")
-    set(CPACK_DEBIAN_PACKAGE_DEPENDS "qt5-default,libavformat58,libavdevice58,libavfilter7,libavutil56,libswscale5,libgl1,ocl-icd-libopencl1")
+    set(CPACK_DEBIAN_PACKAGE_DEPENDS "qt5-default,libavformat58,libavdevice58,libavfilter7,libavutil56,libswscale5,libgl1,ocl-icd-libopencl1,libpng16-16")
     set(CPACK_SET_DESTDIR True)
     set(CPACK_INSTALL_PREFIX "/usr")
     include(CPack)

+ 37 - 8
MandelWidget.cpp

@@ -492,7 +492,7 @@ GridElement* MandelView::searchUnder(int level, GridIndex i, GridIndex j, int re
 }
 
 
-void MandelView::paint(const mnd::MandelViewport& mvp)
+void MandelView::paint(const mnd::MandelViewport& mvp, QPainter& qp)
 {
     mnd::Real dpp = mvp.width / width;
     int level = getLevel(dpp) - 1;
@@ -678,6 +678,18 @@ void MandelWidget::initializeGL(void)
     requestRecalc();
 }
 
+void MandelWidget::resizeGL(int w, int h)
+{
+    double aspect = double(geometry().width()) / geometry().height();
+
+    currentViewport.height = currentViewport.width / aspect;
+    targetViewport = currentViewport;
+
+    if (mandelView.get() != nullptr) {
+        mandelView->width = this->width();
+        mandelView->height = this->height();
+    }
+}
 
 void MandelWidget::paintGL(void)
 {
@@ -691,7 +703,7 @@ void MandelWidget::paintGL(void)
     mandelView->width = width;
     mandelView->height = height;
 
-    glViewport(0, 0, width, height);
+    //glViewport(0, 0, width, height);
 
     glMatrixMode(GL_PROJECTION);
     glLoadIdentity();
@@ -701,12 +713,15 @@ void MandelWidget::paintGL(void)
     glOrtho(0, width, height, 0, -1.0, 1.0);
 #endif
     glMatrixMode(GL_MODELVIEW);
+    glLoadIdentity();
 
     glClear(GL_COLOR_BUFFER_BIT);
-    glLoadIdentity();
 
     updateAnimations();
-    mandelView->paint(this->currentViewport);
+
+    QPainter painter{ this };
+
+    mandelView->paint(this->currentViewport, painter);
 
     if (rubberbanding)
         drawRubberband();
@@ -744,7 +759,15 @@ void MandelWidget::updateAnimations(void)
 
 void MandelWidget::drawRubberband(void)
 {
-    glColor3ub(10, 200, 10);
+    QPainter rubberbandPainter{ this };
+    rubberbandPainter.fillRect(rubberband, QColor{ 25, 225, 25, 50 });
+
+    QPen pen{ QColor{ 10, 200, 10 } };
+    pen.setWidth(2);
+    rubberbandPainter.setPen(pen);
+
+    rubberbandPainter.drawRect(rubberband);
+    /*glColor3ub(10, 200, 10);
     glBegin(GL_LINE_LOOP);
     glVertex2d(rubberband.x(), rubberband.y());
     glVertex2d(rubberband.right(), rubberband.y());
@@ -761,7 +784,7 @@ void MandelWidget::drawRubberband(void)
     glVertex2d(rubberband.right(), rubberband.bottom());
     glVertex2d(rubberband.x(), rubberband.bottom());
     glEnd();
-    glDisable(GL_BLEND);
+    glDisable(GL_BLEND);*/
 }
 
 
@@ -812,14 +835,20 @@ void MandelWidget::drawInfo(void)
 
 void MandelWidget::drawPoint(void)
 {
-    glColor3ub(255, 255, 255);
+    QPainter pointPainter{ this };
+    pointPainter.setPen(QColor{ 255, 255, 255 });
+    pointPainter.drawLine(0, pointY, width(), pointY);
+    pointPainter.drawLine(pointX, 0, pointX, height());
+
+
+    /*glColor3ub(255, 255, 255);
     glBegin(GL_LINES);
     glVertex2f(0, pointY);
     glVertex2f(width(), pointY);
 
     glVertex2f(pointX, 0);
     glVertex2f(pointX, height());
-    glEnd();
+    glEnd();*/
 }
 
 

+ 3 - 1
MandelWidget.h

@@ -7,6 +7,7 @@
 #include <QOpenGLContext>
 #include <QOpenGLFunctions>
 #include <QMutex>
+#include <QPainter>
 //#include <qopengl.h>
 //#include <qopenglfunctions.h>
 //#include <qopenglcontext.h>
@@ -263,7 +264,7 @@ public:
     void garbageCollect(int level, GridIndex i, GridIndex j);
     GridElement* searchAbove(int level, GridIndex i, GridIndex j, int recursionLevel);
     GridElement* searchUnder(int level, GridIndex i, GridIndex j, int recursionLevel);
-    void paint(const mnd::MandelViewport& mvp);
+    void paint(const mnd::MandelViewport& mvp, QPainter& qp);
 public slots:
     void cellReady(int level, GridIndex i, GridIndex j, Bitmap<RGBColor>* bmp);
 signals:
@@ -327,6 +328,7 @@ public:
     void clearAll(void);
 
     void initializeGL(void) override;
+    void resizeGL(int w, int h) override;
     void paintGL() override;
 
 private:

+ 2 - 76
main.cpp

@@ -3,78 +3,8 @@
 #include <QPixmap>
 #include <QDesktopWidget>
 #include <QSplashScreen>
-//#include <QTimer>
 #include <cmath>
 
-class AlmondSplashScreen : public QSplashScreen
-{
-private:
-    float animOff = 0.0f;
-    //QTimer animUpdate;
-    volatile bool updated = true;
-public:
-    AlmondSplashScreen(QPixmap splash) :
-        QSplashScreen{ splash }
-        //animUpdate{ this }
-    {
-        //animUpdate.start(10);
-        //loading.start();
-        //this->add(loading);
-        //connect(&loading, &QMovie::updated, this, &AlmondSplashScreen::nextFrame);
-        //connect(&animUpdate, &QTimer::timeout, this, &AlmondSplashScreen::nextFrame);
-    }
-
-    ~AlmondSplashScreen(void)
-    {
-        //animUpdate.stop();
-    }
-
-    void drawContents(QPainter* painter) override
-    {
-        QSplashScreen::drawContents(painter);
-        //drawAnimation(painter);
-        //updated = true;
-    }
-
-    void drawAnimation(QPainter* painter)
-    {
-        const auto minimum = [] (auto a, auto b, auto c) {
-            return a < b ? (a < c ? a : c) : (b < c ? b : c);
-        };
-        int width = this->width();
-        int height = this->height();
-        int pieces = 7;
-        float off = ::fmod(animOff, width / pieces);
-        for (int i = 0; i < pieces; i++) {
-            float x = off + i * width / pieces;
-            float accelOff = 0;
-
-            if (x < 160)
-                accelOff = (160 - x) * (160 - x) / 160 ;
-            else if (x > width - 160)
-                accelOff = -(width - 160 - x) * (width - 160 - x) / 160 ;
-            
-            x -= accelOff;
-
-            if (x < 0 || x > width)
-                continue;
-
-            float opacity = minimum(x, width - x, 130);
-            QPen pen(QColor(255, 255, 255, int(opacity)));
-            pen.setWidth(4);
-            painter->setPen(pen);
-            painter->drawEllipse(QRectF{ x, double(height - 40), 16, 16 });
-        }
-    }
-
-public slots:
-    void nextFrame() //(const QRect& rect)
-    {
-        emit this->repaint();
-        //animOff += 3;
-    }
-};
-
 int main(int argc, char *argv[])
 {
     QApplication a(argc, argv);
@@ -84,19 +14,15 @@ int main(int argc, char *argv[])
 
     QPixmap splashImg(":/splash/splash");
     QPixmap splashScaled = splashImg.scaled(splashW, splashW * splashImg.height() / splashImg.width());
-    AlmondSplashScreen splash{ splashScaled };
+    QSplashScreen splash{ splashScaled };
     a.processEvents();
     splash.show();
     a.processEvents();
-    /*for (int i = 0; i < 100; i++) {
-        a.processEvents();
-        system("sleep 0.03");
-    }*/
     Almond w;
     a.processEvents();
     splash.finish(&w);
     a.processEvents();
-    w.show();
+    w.showMaximized();
     return a.exec();
 }