screenshot: refactoring 35/36235/3
authorGiWoong Kim <giwoong.kim@samsung.com>
Tue, 3 Mar 2015 06:56:46 +0000 (15:56 +0900)
committerGiWoong Kim <giwoong.kim@samsung.com>
Wed, 4 Mar 2015 05:05:12 +0000 (14:05 +0900)
fix refresh bug when rotating
align screen shot on Scene
modified some variable names
delete unused variables & etc

Change-Id: If902928ec704bf0746e17a42d1c8360c87ed0d9b
Signed-off-by: GiWoong Kim <giwoong.kim@samsung.com>
tizen/src/ui/mainwindow.h
tizen/src/ui/menu/contextmenu.cpp
tizen/src/ui/menu/screenshotdialog.cpp
tizen/src/ui/menu/screenshotdialog.h
tizen/src/ui/menu/screenshotview.cpp
tizen/src/ui/menu/screenshotview.h

index 346d2dfe6bcddf773535e4a6eaf33e5054248727..98da3f677af3224884a62fe38639a8e86369b28c 100644 (file)
@@ -100,13 +100,14 @@ protected:
     void closeEvent(QCloseEvent *);
 
     void setRegion();
+
     QLabel *screenWidget;
 
 private:
     /* windowing */
     QHBoxLayout *winLayout;
     QGraphicsScene *skinScene;
-    SkinViewskinView;
+    SkinView *skinView;
     DisplayBase *display;
     ContextMenu *popupMenu;
     QThread *swapperThread;
index 7426ebc51e77385f936e61429d93381934878a21..d5f75c27a2d79613444f0acb0782ca28d738aae5 100644 (file)
@@ -733,7 +733,7 @@ void ContextMenu::slotShowScreenshot(const QPixmap &screenshot)
     qDebug("show screenshot");
 
     if (screenshotDialog == NULL) {
-        screenshotDialog = new ScreenShotDialog(parent, screenshot);
+        screenshotDialog = new ScreenShotDialog(parent);
     }
 
     screenshotDialog->slotRefresh(screenshot);
index 0fb1c811fa3ef8c72addd4a9a2babc33e6f69666..2b92156ecdc21593b689a7a20e9b71b1de9d8cf3 100644 (file)
 #include "screenshotview.h"
 #include "mainwindow.h"
 
-ScreenShotDialog::ScreenShotDialog(QWidget *parent, const QPixmap &pixmap) :
-    QDialog(parent), screenshotPixmap(pixmap)
+ScreenShotDialog::ScreenShotDialog(QWidget *parent) :
+    QDialog(parent)
 {
     this->win = (MainWindow *)parent;
+    this->ratio = 1;
+    this->posX = "0";
+    this->posY = "0";
 
     setWindowTitle("Screen Shot - " + win->uiInfo->vmName +
         " : " + QString::number(get_device_serial_number()));
     setWindowIcon(QIcon(":/icons/screen_shot.png"));
 
-    createItems();
+    dialogLayout = new QGridLayout(this);
+    dialogLayout->setContentsMargins(0, 0, 0, 0);
 
-    setImage();
-
-    toolbar->addAction(saveAct);
-    toolbar->addAction(copyAct);
-    toolbar->addAction(refreshAct);
-    toolbar->addSeparator();
-    toolbar->addWidget(slider);
-    gridlayout->addWidget(toolbar, 0, 0);
-    gridlayout->addWidget(view, 1, 0);
-    gridlayout->addWidget(statusBar, 2, 0);
+    scene = new QGraphicsScene(this);
+    view = new ScreenShotView(scene, this);
+    view->setCornerWidget(new QScrollArea(this));
 
-    setLayout(gridlayout);
+    createItems(dialogLayout);
 
+    setLayout(dialogLayout);
     resize(win->uiInfo->getMainSize());
 }
 
-void ScreenShotDialog::createItems()
+void ScreenShotDialog::createItems(QGridLayout *layout)
 {
-    statusBar = new QStatusBar(this);
-    toolbar = new QToolBar(this);
+    /* === tool bar === */
+    toolBar = new QToolBar(this);
+
+    /* Save */
+    actionSave = new QAction("&Save", this);
+    actionSave->setShortcuts(QKeySequence::Save);
+    actionSave->setIcon(QIcon(":/icons/save_screenshot_dialog.png"));
+    actionSave->setToolTip("Save to file");
+    connect(actionSave, SIGNAL(triggered()), this, SLOT(slotSave()));
+    toolBar->addAction(actionSave);
+
+    /* Copy */
+    actionCopy = new QAction("&Copy", this);
+    actionCopy->setShortcuts(QKeySequence::Copy);
+    actionCopy->setIcon(QIcon(":/icons/copy_screenshot_dialog.png"));
+    actionCopy->setToolTip("Copy to clipboard");
+    connect(actionCopy, SIGNAL(triggered()), this, SLOT(slotCopy()));
+    toolBar->addAction(actionCopy);
+
+    /* Refresh */
+    actionRefresh = new QAction("&Refresh", this);
+    actionRefresh->setShortcuts(QKeySequence::Refresh);
+    actionRefresh->setIcon(QIcon(":/icons/refresh_screenshot_dialog.png"));
+    actionRefresh->setToolTip("Refresh Image");
+    connect(actionRefresh, SIGNAL(triggered()),
+        win->getPopupMenu(), SLOT(slotRequestScreenshot()));
+    toolBar->addAction(actionRefresh);
 
+    toolBar->addSeparator();
+
+    /* zoom in/out */
     slider = new QSlider(Qt::Horizontal, this);
+    slider->setFixedWidth(100);
     slider->setTickPosition(QSlider::TicksAbove);
     slider->setRange(0, 5);
-    slider->setValue(3);
-    sliderLevel = 3;
-    slider->setFixedWidth(100);
-    slider->setToolTip("100%");
-    connect(slider, SIGNAL(valueChanged(int)), this, SLOT(slotScaleChanged(int)));
-
-    gridlayout = new QGridLayout(this);
-    gridlayout->setContentsMargins(0, 0, 0, 0);
-
-    saveAct = new QAction(tr("&Save"), this);
-    saveAct->setShortcuts(QKeySequence::Save);
-    saveAct->setIcon(QIcon(":/icons/save_screenshot_dialog.png"));
-    saveAct->setToolTip("Save to file");
-    connect(saveAct, SIGNAL(triggered()), this, SLOT(slotSave()));
-
-    copyAct = new QAction(tr("&Copy"), this);
-    copyAct->setShortcuts(QKeySequence::Copy);
-    copyAct->setIcon(QIcon(":/icons/copy_screenshot_dialog.png"));
-    copyAct->setToolTip("Copy to clipboard");
-    connect(copyAct, SIGNAL(triggered()), this, SLOT(slotCopy()));
-
-    refreshAct = new QAction(tr("&Refresh"), this);
-    refreshAct->setShortcuts(QKeySequence::Refresh);
-    refreshAct->setIcon(QIcon(":/icons/refresh_screenshot_dialog.png"));
-    refreshAct->setToolTip("Refresh Image");
-    connect(refreshAct, SIGNAL(triggered()),
-        win->getPopupMenu(), SLOT(slotRequestScreenshot()));
-}
 
-void ScreenShotDialog::setImage()
-{
-    scene = new QGraphicsScene(this);
+    slider->setValue(3); /* default */
+    updateRatio(getSliderLevel());
 
-    qDebug() << screenshotPixmap;
+    connect(slider, SIGNAL(valueChanged(int)), this, SLOT(slotSliderChanged(int)));
+    toolBar->addWidget(slider);
 
-    QMatrix rm;
-    rm.rotate(getRotateAngle());
-    screenshotPixmap = screenshotPixmap.transformed(rm);
-    scene->addPixmap(screenshotPixmap);
+    layout->addWidget(toolBar, 0, 0);
+    /* ====== */
 
-    view = new ScreenShotView(scene, this);
-    ratioStr = "100%";
-    view->setScene(scene);
-    view->setMouseTracking(true);
-
-    QScrollArea *area = new QScrollArea(this);
-    area->setFixedSize(screenshotPixmap.width(), screenshotPixmap.height());
-    view->setCornerWidget(area);
-    view->verticalScrollBar()->setValue(1);
-    view->horizontalScrollBar()->setValue(1);
+    /* view */
+    layout->addWidget(view, 1, 0);
+
+    /* status bar */
+    statusBar = new QStatusBar(this);
+    layout->addWidget(statusBar, 2, 0);
 }
 
-int ScreenShotDialog::getRotateAngle()
+// TODO: UIState
+int ScreenShotDialog::getDisplayAngle()
 {
     return win->uiInfo->getMainFormDisplayType()->getAngle();
 }
 
-QString ScreenShotDialog::getRatio()
+QString ScreenShotDialog::getRatioInfo()
 {
-    return ratioStr;
+    return QString::number(ratio * 100).append("%");
 }
 
-void ScreenShotDialog::setRatio(int level)
+QPixmap &ScreenShotDialog::getShotData()
 {
-    switch (level) {
-    case 0:
-        ratio = 0.125;
-        break;
-    case 1:
-        ratio = 0.25;
-        break;
-    case 2:
-        ratio = 0.50;
-        break;
-    case 3:
-        ratio = 1;
-        break;
-    case 4:
-        ratio = 2;
-        break;
-    case 5:
-        ratio = 4;
-        break;
-    }
-    ratioStr = QString::number(ratio * 100).append("%");
+    return shotData;
 }
 
 int ScreenShotDialog::getSliderLevel()
 {
-    return sliderLevel;
+    return slider->value();
 }
 
 bool ScreenShotDialog::slotSave()
@@ -178,7 +152,7 @@ bool ScreenShotDialog::slotSave()
         return false;
     }
 
-    QPixmap pixmap(screenshotPixmap);
+    QPixmap pixmap(shotData);
     qDebug() << filename;
     pixmap.save(filename);
 
@@ -189,47 +163,85 @@ void ScreenShotDialog::slotCopy()
 {
     qDebug("copy to clipboard");
 
-    QClipboard* clipboard = QApplication::clipboard();
-    QPixmap pixmap(screenshotPixmap);
-    clipboard->clear();
-    clipboard->setPixmap(pixmap);
+    QClipboard *clipboard = QApplication::clipboard();
+    if (clipboard != NULL) {
+        clipboard->clear();
+        clipboard->setPixmap(shotData);
+    } else {
+        qWarning("cannot copy to clipboard");
+    }
 }
 
 void ScreenShotDialog::slotRefresh(const QPixmap &pixmap)
 {
     qDebug("refresh");
 
-    frameBuf = pixmap;
+    shotData = pixmap;
 
-    ratioStr = "100%";
-    sliderLevel = 3;
-    slider->setValue(3);
     scene->clear();
 
-    // TODO: once
-    QMatrix rm;
-    rm.rotate(getRotateAngle());
-    screenshotPixmap = screenshotPixmap.transformed(rm);
-    scene->addPixmap(screenshotPixmap);
-    slider->setToolTip(ratioStr);
+    /* draw a screenshot */
+    updateScreenShot(shotData);
 }
 
-void ScreenShotDialog::slotScaleChanged(int level)
+void ScreenShotDialog::slotSliderChanged(int level)
 {
-    qDebug("scale changed: %d", level);
+    qDebug("slider changed : %d", level);
+
+    updateRatio(getSliderLevel());
 
-    sliderLevel = level;
-    QPixmap pixmap(screenshotPixmap);
-    setRatio(level);
     scene->clear();
-    scene->addPixmap(pixmap.scaled(pixmap.size() * ratio,
-        Qt::KeepAspectRatio, Qt::FastTransformation));
-    slider->setToolTip(ratioStr);
+
+    /* draw a screenshot */
+    updateScreenShot(shotData);
 
     updateStatusBar();
 }
 
-void ScreenShotDialog::setStatusBar(qreal xx, qreal yy)
+void ScreenShotDialog::updateRatio(int level)
+{
+    // TODO: enum
+    switch (level) {
+    case 0:
+        ratio = 0.125;
+        break;
+    case 1:
+        ratio = 0.25;
+        break;
+    case 2:
+        ratio = 0.50;
+        break;
+    case 3:
+        ratio = 1;
+        break;
+    case 4:
+        ratio = 2;
+        break;
+    case 5:
+        ratio = 4;
+        break;
+    }
+
+    slider->setToolTip(getRatioInfo());
+}
+
+void ScreenShotDialog::updateScreenShot(QPixmap &shotData)
+{
+    /* scaling */
+    QPixmap shotImage = shotData.scaled(shotData.size() * ratio,
+        Qt::KeepAspectRatio, Qt::FastTransformation);
+
+    /* rotate */
+    QMatrix matrix;
+    matrix.rotate(getDisplayAngle());
+    shotImage = shotImage.transformed(matrix);
+
+    /* update */
+    scene->setSceneRect(0, 0, shotImage.width(), shotImage.height());
+    scene->addPixmap(shotImage);
+}
+
+void ScreenShotDialog::updateMousePosition(int xx, int yy)
 {
     posX = QString::number(xx);
     posY = QString::number(yy);
@@ -239,16 +251,20 @@ void ScreenShotDialog::setStatusBar(qreal xx, qreal yy)
 
 void ScreenShotDialog::updateStatusBar()
 {
-    statusBar->showMessage("x: " + posX + ", y:" + posY +
-        " (" + QString::number(screenshotPixmap.width()) +
-        "x" + QString::number(screenshotPixmap.height()) +
-        ", " + qPrintable(getRatio()) + ")");
+    if (statusBar != NULL) {
+        statusBar->showMessage("x: " + posX + ", y:" + posY +
+            " (" + QString::number(shotData.width()) +
+            "x" + QString::number(shotData.height()) +
+            ", " + getRatioInfo() + ")");
+    }
 }
 
 void ScreenShotDialog::showEvent(QShowEvent *event)
 {
     Q_UNUSED(event)
 
+    updateStatusBar();
+
     // TODO:
     move(win->geometry().center().x(),
         win->geometry().center().y() - (geometry().size().height() / 2));
index 87bd1570d675afb344ac83c57fc37e86c0739cd7..d030fdd172eeaf17d4a79f2446ff2ee45f83d0c5 100644 (file)
@@ -40,15 +40,15 @@ class ScreenShotDialog : public QDialog
     Q_OBJECT
 
 public:
-    explicit ScreenShotDialog(QWidget *parent, const QPixmap &screenshot);
+    explicit ScreenShotDialog(QWidget *parent);
     ~ScreenShotDialog();
 
-    int getRotateAngle();
-    QString getRatio();
+    int getDisplayAngle();
+    QString getRatioInfo();
+    QPixmap &getShotData();
     int getSliderLevel();
-    void setStatusBar(qreal xx, qreal yy);
-
-    QPixmap frameBuf;
+    void updateMousePosition(int xx, int yy);
+    void updateStatusBar();
 
 public slots:
     bool slotSave();
@@ -58,38 +58,31 @@ public slots:
 protected:
     void showEvent(QShowEvent *event);
 
-    QGraphicsScene *scene;
-
 private:
-    void createItems();
-    void setImage();
-    void setRatio(int level);
-    void updateStatusBar();
+    void createItems(QGridLayout *layout);
+    void updateRatio(int level);
+    void updateScreenShot(QPixmap &shotData);
 
     MainWindow *win;
+    QGridLayout *dialogLayout;
+    QGraphicsScene *scene;
+    QGraphicsView *view;
+
+    /* state */
+    QPixmap shotData; /* framebuffer */
+    float ratio;
     QString posX;
     QString posY;
-    QGridLayout *gridlayout;
-    QGraphicsView *view;
-    int sliderLevel;
-    QLabel *label;
-    QSlider *slider;
-    QPixmap screenshotPixmap;
-    QToolBar *toolbar;
 
-    QStatusBar *statusBar;
-    QMenu *fileMenu;
-    QMenu *editMenu;
-    QMenu *helpMenu;
     QToolBar *toolBar;
-    float ratio;
-    QString ratioStr;
-    QAction *saveAct;
-    QAction *copyAct;
-    QAction *refreshAct;
+    QAction *actionSave;
+    QAction *actionCopy;
+    QAction *actionRefresh;
+    QSlider *slider;
+    QStatusBar *statusBar;
 
 private slots:
-    void slotScaleChanged(int level);
+    void slotSliderChanged(int level);
 };
 
 #endif // SCREENSHOT_H
index 987744462e423cb5404121f80f33538c27e5dc5b..10163f4c2a45cf27b65794eb765ee0043536f9fe 100644 (file)
@@ -36,33 +36,35 @@ ScreenShotView::ScreenShotView(QGraphicsScene *scene, QWidget *parent) :
     QGraphicsView(scene, parent)
 {
     setBackgroundRole(QPalette::Dark);
+
+    setMouseTracking(true);
 }
 
 void ScreenShotView::mouseMoveEvent(QMouseEvent *event)
 {
+    ScreenShotDialog *screenshot = (ScreenShotDialog *)parent();
+
     int max_x;
     int max_y;
-    ScreenShotDialog *screenshot = (ScreenShotDialog *)(this->parent());
-    QPointF fixedPos = this->mapToScene(event->pos());
+
+    // TODO: radian
+    QPointF fixedPos = mapToScene(event->pos());
     int sliderLevel = screenshot->getSliderLevel();
 
-    switch (screenshot->getRotateAngle()) {
+    switch (screenshot->getDisplayAngle()) {
     case 90:
     case 270:
-        max_x = screenshot->frameBuf.size().height();
-        max_y = screenshot->frameBuf.size().width();
+        max_x = screenshot->getShotData().size().height();
+        max_y = screenshot->getShotData().size().width();
         break;
     case 0:
     case 180:
     default:
-        max_x = screenshot->frameBuf.size().width();
-        max_y = screenshot->frameBuf.size().height();
+        max_x = screenshot->getShotData().size().width();
+        max_y = screenshot->getShotData().size().height();
         break;
     }
 
-    // Too many logs are printed while mouse is moved.
-    // Thus comment out this line.
-    //qDebug("sliderLevel: %d", sliderLevel);
     int x;
     int y;
     float expo = 0;
@@ -99,7 +101,7 @@ void ScreenShotView::mouseMoveEvent(QMouseEvent *event)
     }
 
     if (scene()->sceneRect().contains(fixedPos)) {
-        screenshot->setStatusBar(x, y);
+        screenshot->updateMousePosition(x, y);
     }
 }
 
index a112e6662a505ba61657180edd4e6d9468fb7200..e5ee78c3efd6b33279cf09d6e9c689e13ce07aef 100644 (file)
@@ -30,7 +30,7 @@
 #ifndef SCREENSHOTVIEW_H
 #define SCREENSHOTVIEW_H
 
-#include <QtWidgets>
+#include <QGraphicsView>
 
 class ScreenShotView: public QGraphicsView
 {