rotary: added rotary Qt UI
authorGiWoong Kim <giwoong.kim@samsung.com>
Wed, 6 May 2015 10:45:19 +0000 (19:45 +0900)
committerGiWoong Kim <giwoong.kim@samsung.com>
Thu, 7 May 2015 02:17:15 +0000 (11:17 +0900)
Change-Id: I3f7ee4a9882cc17f3cf957a4227413d9bb0fcfe6
Signed-off-by: GiWoong Kim <giwoong.kim@samsung.com>
tizen/src/display/xmllayoutparser.cpp
tizen/src/ui/Makefile.objs
tizen/src/ui/displaybase.cpp
tizen/src/ui/layout/mainform.cpp
tizen/src/ui/layout/mainform.h
tizen/src/ui/mainwindow.cpp
tizen/src/ui/mainwindow.h
tizen/src/ui/rotaryview.cpp [new file with mode: 0644]
tizen/src/ui/rotaryview.h [new file with mode: 0644]

index dba4d35..267fd49 100644 (file)
@@ -277,8 +277,9 @@ MainForm *XmlLayoutParser::parseMainForm(QXmlStreamReader &xml)
                 QString rotaryImageFileName = xml.readElementText();
                 qDebug() << "-" << ROTARY_IMG_KEYWORD << ":" << rotaryImageFileName;
 
-                if (form->rotaryImg.load(
-                    xmlPath + QDir::separator() + rotaryImageFileName) == false) {
+                form->setRotaryImage(new QPixmap(
+                    xmlPath + QDir::separator() + rotaryImageFileName));
+                if (form->getRotaryImage()->isNull() == true) {
                     qWarning() << "failed to load rotary image";
                 }
             } else if (xml.name() == KEYLIST_KEYWORD) {
index e4c2e4d..1ca3f7c 100644 (file)
@@ -12,6 +12,7 @@ obj-$(CONFIG_QT) += mainwindow.o moc_mainwindow.o
 obj-$(CONFIG_QT) += skinbezelitem.o
 obj-$(CONFIG_QT) += skinkeyitem.o moc_skinkeyitem.o
 obj-$(CONFIG_QT) += skinview.o
+obj-$(CONFIG_QT) += rotaryview.o
 obj-$(CONFIG_QT) += skinpainter.o
 obj-$(CONFIG_QT) += uiinformation.o
 obj-$(CONFIG_QT) += uistate.o
index 0591e2b..c38b7a1 100644 (file)
@@ -85,11 +85,6 @@ void DisplayBase::updateGeometry()
         rect.y() * scaleFactor,
         rect.width() * scaleFactor,
         rect.height() * scaleFactor);
-
-    if (maskImage.size() != QSize(0, 0)) {
-        qDebug("set a mask to display");
-        widget->setMask(maskImage.mask());
-    }
 }
 
 void DisplayBase::handlePaint(QPaintEvent *event)
@@ -105,6 +100,12 @@ void DisplayBase::handleResize(QResizeEvent *event)
     qt5_window_height = widget->height();
 
     qt5_graphic_hw_invalidate();
+
+    /* masking */
+    if (maskImage.size() != QSize(0, 0)) {
+        qDebug("set a mask to display");
+        widget->setMask(maskImage.mask());
+    }
 }
 
 QPoint DisplayBase::getGuestPos(QPoint hostPos)
index 3076e7b..68ff468 100644 (file)
 MainForm::MainForm(const QString &name) : LayoutForm(name)
 {
     this->displayType = NULL;
+    this->rotaryImg = NULL;
+}
+
+void MainForm::setRotaryImage(QPixmap *image)
+{
+    rotaryImg = image;
+}
+
+QPixmap *MainForm::getRotaryImage()
+{
+    return rotaryImg;
 }
 
 MainForm::~MainForm()
@@ -42,6 +53,10 @@ MainForm::~MainForm()
         delete displayType;
     }
 
+    if (rotaryImg != NULL) {
+        delete rotaryImg;
+    }
+
     for (int i = 0; i < keyList.count(); i++) {
         delete keyList.at(i);
     }
index fd097f8..9e0e288 100644 (file)
@@ -44,15 +44,20 @@ public:
     MainForm(const QString &name);
     ~MainForm();
 
+    void setRotaryImage(QPixmap *image);
+    QPixmap *getRotaryImage();
+
     DisplayType *displayType;
     QImage skinImg[2];
-    QImage rotaryImg;
     QList<HardwareKey *> keyList;
 
     enum SkinImgType {
         normal = 0,
         pressed = 1
     };
+
+private:
+    QPixmap *rotaryImg;
 };
 
 #endif // MAINFORM_H
index 8ef3f2c..16084f7 100644 (file)
@@ -79,6 +79,7 @@ MainWindow::MainWindow(UIInformation *uiInfo, QWidget *parent) :
     this->popupMenu = NULL;
 
     this->display = NULL;
+    this->rotary = NULL;
     this->swapper = NULL;
     this->swapperThread = NULL;
 
@@ -105,6 +106,21 @@ MainWindow::MainWindow(UIInformation *uiInfo, QWidget *parent) :
     skinView = new SkinView(skinScene, this);
     winLayout->addWidget(skinView);
 
+    /* rotary */
+    MainForm *form = NULL;
+    bool needRotary = false;
+    for(int i = 0; i < uiInfo->mainFormList.count(); i++) {
+        form = uiInfo->mainFormList.at(i);
+        if (form->getRotaryImage() != NULL &&
+            form->getRotaryImage()->isNull() == false) {
+            needRotary = true;
+            break;
+        }
+    }
+    if (needRotary == true) {
+        rotary = createRotary();
+    }
+
     /* display */
     display = createDisplay(uiInfo->getMainFormDisplayType());
 
@@ -122,6 +138,8 @@ MainWindow::MainWindow(UIInformation *uiInfo, QWidget *parent) :
 
 DisplayBase *MainWindow::createDisplay(DisplayType *displayForm)
 {
+    qDebug("create a display");
+
     DisplayBase *displayWidget = NULL;
 
     if (qt5GLContext) { /* on-screen rendering */
@@ -142,7 +160,7 @@ DisplayBase *MainWindow::createDisplay(DisplayType *displayForm)
          */
         QGLContext *context = new QGLContext(format);
 
-        displayWidget = new DisplayGLWidget(skinView, context,
+        displayWidget = new DisplayGLWidget(this, context,
             displayForm, getUIState()->getScaleFactor());
 
         context->setFormat(format);
@@ -162,7 +180,7 @@ DisplayBase *MainWindow::createDisplay(DisplayType *displayForm)
 
         swapperThread->start();
     } else { /* off-screen rendering */
-        DisplaySWWidget *widget = new DisplaySWWidget(skinView,
+        DisplaySWWidget *widget = new DisplaySWWidget(this,
             displayForm, getUIState()->getScaleFactor());
 
         screenWidget = widget;
@@ -172,6 +190,12 @@ DisplayBase *MainWindow::createDisplay(DisplayType *displayForm)
     return displayWidget;
 }
 
+RotaryView *MainWindow::createRotary()
+{
+    qDebug("create a rotary");
+    return new RotaryView(this);
+}
+
 void MainWindow::startDisplaySwapper()
 {
     if (swapper != NULL) {
@@ -372,7 +396,11 @@ void MainWindow::switchForm(int index)
     }
 
     resize(uiInfo->getUiSize());
+
     skinView->update();
+    if (rotary != NULL) {
+        rotary->update();
+    }
 
     if (display != NULL) {
         display->switchForm(uiInfo->getMainFormDisplayType());
@@ -392,7 +420,11 @@ void MainWindow::scaleForm(int scale)
     }
 
     resize(uiInfo->getUiSize());
+
     skinView->update();
+    if (rotary != NULL) {
+        rotary->update();
+    }
 
     if (display != NULL) {
         display->scaleForm(getUIState()->getScaleFactor());
@@ -462,6 +494,11 @@ MainWindow::~MainWindow()
 
     swapperThread->quit();
     swapperThread->wait();
+
+    if (rotary != NULL) {
+        delete rotary;
+        rotary = NULL;
+    }
 }
 
 void MainWindow::closeEvent(QCloseEvent *event)
index ef5edcf..8376cf5 100644 (file)
@@ -39,7 +39,7 @@
 #include "menu/contextmenu.h"
 #include "skinview.h"
 #include "displaybase.h"
-#include "skinbezelitem.h"
+#include "rotaryview.h"
 #include "uiinformation.h"
 #include "controller/dockingcontroller.h"
 #include "controller/floatingcontroller.h"
@@ -109,12 +109,15 @@ protected:
 
 private:
     DisplayBase *createDisplay(DisplayType *displayForm);
+    RotaryView *createRotary();
 
     /* windowing */
     QHBoxLayout *winLayout;
     QGraphicsScene *skinScene;
     SkinView *skinView;
     DisplayBase *display;
+    RotaryView *rotary;
+
     ContextMenu *popupMenu;
     QThread *swapperThread;
     DisplaySwapper *swapper;
diff --git a/tizen/src/ui/rotaryview.cpp b/tizen/src/ui/rotaryview.cpp
new file mode 100644 (file)
index 0000000..e4596d5
--- /dev/null
@@ -0,0 +1,165 @@
+/*
+ * Qt UI
+ *
+ * Copyright (C) 2015 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Contact:
+ * GiWoong Kim <giwoong.kim@samsung.com>
+ * Sangho Park <sangho1206.park@samsung.com>
+ *
+ * This program 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 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program 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 this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ * MA 02110-1301, USA.
+ *
+ * Contributors:
+ * - S-Core Co., Ltd
+ *
+ */
+
+#include <QWidget>
+
+#include "rotaryview.h"
+#include "mainwindow.h"
+
+RotaryView::RotaryView(QWidget *parent) :
+    QGraphicsView(new QGraphicsScene(parent), parent)
+{
+    this->win = (MainWindow *)parent;
+    this->rotaryImg = win->uiInfo->getMainForm()->getRotaryImage();
+    this->grabPos = QPoint(-1, -1);
+    this->grabAngle = 0;
+    this->rotaryDegree = 0;
+
+    setStyleSheet("background-color: rgb(189, 189, 189); border-style: none");
+    setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
+    setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
+    setAlignment(Qt::AlignLeft | Qt::AlignTop);
+
+    update();
+}
+
+void RotaryView::createItems()
+{
+    rotaryItem = new QGraphicsPixmapItem(*rotaryImg);
+    rotaryItem->setTransformOriginPoint(
+        rotaryImg->width() / 2, rotaryImg->height() / 2);
+    rotaryItem->setRotation(rotaryDegree);
+    scene()->addItem(rotaryItem);
+}
+
+void RotaryView::update()
+{
+    scene()->clear();
+
+    rotaryImg = win->uiInfo->getMainForm()->getRotaryImage();
+    const QRect displayRect = win->uiInfo->getMainFormDisplayType()->getRect();
+
+    /* repositioning & resizing */
+    const int alignOffsetX = (rotaryImg->width() - displayRect.width()) / 2;
+    const int alignOffsetY = (rotaryImg->height() - displayRect.height()) / 2;
+
+    setGeometry(
+        displayRect.x() - alignOffsetX,
+        displayRect.y() - alignOffsetY,
+        rotaryImg->width(), rotaryImg->height());
+    scene()->setSceneRect(0, 0, width(), height());
+
+    createItems();
+}
+
+/* override */
+void RotaryView::resizeEvent(QResizeEvent *event)
+{
+    qDebug() << "resize rotary view :" << size();
+
+    /* scaling */
+    const qreal sx = win->getUIState()->getScaleFactor();
+    const qreal sy = win->getUIState()->getScaleFactor();
+    setTransform(QTransform(sx, 0, 0, 0, sy, 0, 0, 0, 1));
+
+    /* masking */
+    setMask(win->uiInfo->getMainForm()->getRotaryImage()->mask());
+
+    QGraphicsView::resizeEvent(event);
+}
+
+/* override */
+void RotaryView::setMask(const QRegion &region)
+{
+    if (region.isEmpty() == false) {
+        QWidget::setMask(region);
+    }
+}
+
+int RotaryView::getAngleFromVector(QPoint pos)
+{
+    return (int)(qAtan2(
+        pos.y() - (height() / 2), pos.x() - (width() / 2)) *
+        float(180 / M_PI) /* radians to degress */);
+}
+
+/* override */
+void RotaryView::mousePressEvent(QMouseEvent *event)
+{
+    if (event->button() == Qt::LeftButton) {
+        grabPos = event->pos();
+        grabAngle = getAngleFromVector(grabPos);
+    }
+
+    QGraphicsView::mousePressEvent(event);
+}
+
+/* override */
+void RotaryView::mouseReleaseEvent(QMouseEvent *event)
+{
+    if (event->button() == Qt::LeftButton) {
+        grabPos = QPoint(-1, -1);
+        grabAngle = 0;
+    }
+
+    QGraphicsView::mouseReleaseEvent(event);
+}
+
+/* override */
+void RotaryView::mouseMoveEvent(QMouseEvent *event)
+{
+    if (grabPos != QPoint(-1, -1)) {
+        const int moveAngle = getAngleFromVector(event->pos());
+
+        int deltaAngle = moveAngle - grabAngle;
+        if (deltaAngle < -180) {
+            deltaAngle += 360; /* 359 -> 0 */
+        } else if (deltaAngle > 180) {
+            deltaAngle -= 360; /* 0 -> 359 */
+        }
+        qDebug() << "rotary delta :" << deltaAngle;
+
+        rotaryDegree += deltaAngle;
+
+        /* TODO: too many heavy calls over here
+        move to thread or paintEvent handler */
+        rotaryItem->setRotation(rotaryDegree);
+
+        grabAngle = moveAngle;
+    }
+
+    QGraphicsView::mouseMoveEvent(event);
+}
+
+RotaryView::~RotaryView()
+{
+    qDebug("destroy rotary view");
+
+    scene()->clear();
+}
diff --git a/tizen/src/ui/rotaryview.h b/tizen/src/ui/rotaryview.h
new file mode 100644 (file)
index 0000000..713b8e7
--- /dev/null
@@ -0,0 +1,67 @@
+/*
+ * Qt UI
+ *
+ * Copyright (C) 2015 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Contact:
+ * GiWoong Kim <giwoong.kim@samsung.com>
+ * Sangho Park <sangho1206.park@samsung.com>
+ *
+ * This program 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 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program 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 this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ * MA 02110-1301, USA.
+ *
+ * Contributors:
+ * - S-Core Co., Ltd
+ *
+ */
+
+#ifndef ROTARYVIEW_H
+#define ROTARYVIEW_H
+
+#include <QGraphicsView>
+
+class MainWindow;
+
+class RotaryView : public QGraphicsView
+{
+public:
+    RotaryView(QWidget *parent);
+    ~RotaryView();
+
+    void update();
+
+protected:
+    void resizeEvent(QResizeEvent *event);
+    void setMask(const QRegion &region);
+
+    void mousePressEvent(QMouseEvent *event);
+    void mouseReleaseEvent(QMouseEvent *event);
+    void mouseMoveEvent(QMouseEvent *event);
+
+private:
+    void createItems();
+    int getAngleFromVector(QPoint pos);
+
+    MainWindow *win;
+    QPixmap *rotaryImg;
+    QGraphicsPixmapItem *rotaryItem;
+
+    QPoint grabPos;
+    int grabAngle;
+    int rotaryDegree; /* absolute */
+
+};
+
+#endif // ROTARYVIEW_H