...
[jspeed] / src / graphicsscreen.cpp
1 /*
2  * This file is part of jSpeed.
3  *
4  * jSpeed is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * jSpeed is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with jSpeed.  If not, see <http://www.gnu.org/licenses/>.
16  *
17  */
18
19 #include <QtCore/QDebug>
20 #include <QtGui/QApplication>
21 #include <QtGui/QDesktopWidget>
22 #include "graphicsscreen.h"
23 #include "toolbaritem.h"
24 #include "graphicsscene.h"
25 #include "odometer.h"
26
27 namespace
28 {
29     int const PADDING = 6;
30     QString const BACKGROUND_COLOR = "#000";
31 }
32
33 GraphicsScreen::GraphicsScreen(QWidget* parent): QGraphicsView(parent),
34 AbstractScreen(), scene_(0), isFlipped_(false)
35 {
36     QRect rect = QApplication::desktop()->availableGeometry();
37
38     scene_ = new GraphicsScene(QRect(QPoint(0, 0), rect.size()));
39     setScene(scene_);
40
41     setBackgroundBrush(QBrush(QColor(BACKGROUND_COLOR)));
42
43     connect(scene_, SIGNAL(clicked()), this, SIGNAL(clicked()));
44
45     minimizeButton_ = new ToolbarItem(":/resources/minimize.png", ":/resources/minimize_active.png");
46     minimizeButton_->setZValue(9999);
47     scene_->addItem(minimizeButton_);
48     connect(minimizeButton_, SIGNAL(clicked()), this, SIGNAL(minimizePressed()));
49
50     imageWidth_ = minimizeButton_->pixmap().width();
51
52     closeButton_ = new ToolbarItem(":/resources/close.png", ":/resources/close_active.png");
53     closeButton_->setZValue(9999);
54     scene_->addItem(closeButton_);
55     connect(closeButton_, SIGNAL(clicked()), this, SIGNAL(closePressed()));
56
57     settingsButton_ = new ToolbarItem(":/resources/settings.png", ":/resources/settings_active.png");
58     settingsButton_->setZValue(9999);
59     scene_->addItem(settingsButton_);
60     connect(settingsButton_, SIGNAL(clicked()), this, SIGNAL(settingsPressed()));
61
62     reArrange();
63
64     connect(&(Odometer::instance()), SIGNAL(dataUpdated()), this, SLOT(update()));
65     connect(&(Odometer::instance()), SIGNAL(unitChanged()), this, SLOT(update()));
66 }
67
68 GraphicsScreen::~GraphicsScreen()
69 {
70 }
71
72 void GraphicsScreen::reArrange()
73 {
74     QRect rect = QApplication::desktop()->screenGeometry();
75     scene_->setSceneRect(rect);
76     minimizeButton_->setPos(PADDING, PADDING);
77     closeButton_->setPos(rect.width() - imageWidth_ - PADDING, PADDING);
78     settingsButton_->setPos((rect.width() / 2) - (imageWidth_ / 2), PADDING);
79 }
80
81 void GraphicsScreen::flip()
82 {
83     if(isFlipped_)
84     {
85         resetMatrix();
86         isFlipped_  = false;
87     }
88     else
89     {
90         setTransform(QTransform(1, 0, 0, 0, -1, 0, 0, 0, 1));
91         isFlipped_ = true;
92     }
93 }
94
95 GraphicsScene* GraphicsScreen::getScene() const
96 {
97     return scene_;
98 }
99
100 void GraphicsScreen::forceRepaint()
101 {
102     QApplication::processEvents();
103     update();
104     viewport()->update();
105     QApplication::processEvents();
106 }