author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Tue, 06 Jul 2010 15:10:48 +0300 | |
changeset 30 | 5dc02b23752f |
parent 18 | 2f34d5167611 |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 4 |
** All rights reserved. |
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the examples of the Qt Toolkit. |
|
8 |
** |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
9 |
** $QT_BEGIN_LICENSE:BSD$ |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
10 |
** You may use this file under the terms of the BSD license as follows: |
0 | 11 |
** |
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
12 |
** "Redistribution and use in source and binary forms, with or without |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
13 |
** modification, are permitted provided that the following conditions are |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
14 |
** met: |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
15 |
** * Redistributions of source code must retain the above copyright |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
16 |
** notice, this list of conditions and the following disclaimer. |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
17 |
** * Redistributions in binary form must reproduce the above copyright |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
18 |
** notice, this list of conditions and the following disclaimer in |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
19 |
** the documentation and/or other materials provided with the |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
20 |
** distribution. |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
21 |
** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
22 |
** the names of its contributors may be used to endorse or promote |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
23 |
** products derived from this software without specific prior written |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
24 |
** permission. |
0 | 25 |
** |
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
26 |
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
27 |
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
28 |
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
29 |
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
30 |
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
31 |
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
32 |
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
33 |
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
34 |
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
35 |
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
36 |
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
0 | 37 |
** $QT_END_LICENSE$ |
38 |
** |
|
39 |
****************************************************************************/ |
|
40 |
||
41 |
#include "mainwindow.h" |
|
42 |
#include "stationdialog.h" |
|
43 |
||
44 |
#include <QtCore/QSettings> |
|
45 |
#include <QtCore/QTimer> |
|
46 |
#include <QtGui/QAction> |
|
47 |
#include <QtGui/QApplication> |
|
48 |
#include <QtGui/QBitmap> |
|
49 |
#include <QtGui/QLinearGradient> |
|
50 |
#include <QtGui/QMouseEvent> |
|
51 |
#include <QtGui/QPainter> |
|
52 |
||
53 |
MainWindow::MainWindow() |
|
54 |
: QWidget(0, Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint) |
|
55 |
{ |
|
56 |
QAction *quitAction = new QAction(tr("E&xit"), this); |
|
57 |
quitAction->setShortcuts(QKeySequence::Quit); |
|
58 |
connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit())); |
|
59 |
||
60 |
QAction *configAction = new QAction(tr("&Select station..."), this); |
|
61 |
configAction->setShortcut(tr("Ctrl+C")); |
|
62 |
connect(configAction, SIGNAL(triggered()), this, SLOT(configure())); |
|
63 |
||
64 |
addAction(configAction); |
|
65 |
addAction(quitAction); |
|
66 |
||
67 |
setContextMenuPolicy(Qt::ActionsContextMenu); |
|
68 |
||
69 |
setWindowTitle(tr("Traffic Info Oslo")); |
|
70 |
||
71 |
const QSettings settings("Qt Traffic Info", "trafficinfo"); |
|
72 |
m_station = StationInformation(settings.value("stationId", "03012130").toString(), |
|
73 |
settings.value("stationName", "Nydalen [T-bane] (OSL)").toString()); |
|
74 |
m_lines = settings.value("lines", QStringList()).toStringList(); |
|
75 |
||
76 |
QTimer *timer = new QTimer(this); |
|
77 |
connect(timer, SIGNAL(timeout()), this, SLOT(updateTimeInformation())); |
|
78 |
timer->start(1000*60*5); |
|
79 |
QMetaObject::invokeMethod(this, SLOT(updateTimeInformation()), Qt::QueuedConnection); |
|
80 |
} |
|
81 |
||
82 |
MainWindow::~MainWindow() |
|
83 |
{ |
|
84 |
QSettings settings("Qt Traffic Info", "trafficinfo"); |
|
85 |
settings.setValue("stationId", m_station.id()); |
|
86 |
settings.setValue("stationName", m_station.name()); |
|
87 |
settings.setValue("lines", m_lines); |
|
88 |
} |
|
89 |
||
90 |
QSize MainWindow::sizeHint() const |
|
91 |
{ |
|
92 |
return QSize(300, 200); |
|
93 |
} |
|
94 |
||
95 |
void MainWindow::mouseMoveEvent(QMouseEvent *event) |
|
96 |
{ |
|
97 |
if (event->buttons() & Qt::LeftButton) { |
|
98 |
move(event->globalPos() - m_dragPosition); |
|
99 |
event->accept(); |
|
100 |
} |
|
101 |
} |
|
102 |
||
103 |
void MainWindow::mousePressEvent(QMouseEvent *event) |
|
104 |
{ |
|
105 |
if (event->button() == Qt::LeftButton) { |
|
106 |
m_dragPosition = event->globalPos() - frameGeometry().topLeft(); |
|
107 |
event->accept(); |
|
108 |
} |
|
109 |
} |
|
110 |
||
111 |
void MainWindow::paintEvent(QPaintEvent*) |
|
112 |
{ |
|
113 |
const QPoint start(width()/2, 0); |
|
114 |
const QPoint finalStop(width()/2, height()); |
|
115 |
QLinearGradient gradient(start, finalStop); |
|
116 |
const QColor qtGreen(102, 176, 54); |
|
117 |
gradient.setColorAt(0, qtGreen.dark()); |
|
118 |
gradient.setColorAt(0.5, qtGreen); |
|
119 |
gradient.setColorAt(1, qtGreen.dark()); |
|
120 |
||
121 |
QPainter p(this); |
|
122 |
p.fillRect(0, 0, width(), height(), gradient); |
|
123 |
||
124 |
QFont headerFont("Sans Serif", 12, QFont::Bold); |
|
125 |
QFont normalFont("Sans Serif", 9, QFont::Normal); |
|
126 |
||
127 |
// draw it twice for shadow effect |
|
128 |
p.setFont(headerFont); |
|
129 |
QRect headerRect(1, 1, width(), 25); |
|
130 |
p.setPen(Qt::black); |
|
131 |
p.drawText(headerRect, Qt::AlignCenter, m_station.name()); |
|
132 |
||
133 |
headerRect.moveTopLeft(QPoint(0, 0)); |
|
134 |
p.setPen(Qt::white); |
|
135 |
p.drawText(headerRect, Qt::AlignCenter, m_station.name()); |
|
136 |
||
137 |
p.setFont(normalFont); |
|
138 |
int pos = 40; |
|
139 |
for (int i = 0; i < m_times.count() && i < 9; ++i) { |
|
140 |
p.setPen(Qt::black); |
|
141 |
p.drawText(51, pos + 1, m_times.at(i).time()); |
|
142 |
p.drawText(101, pos + 1, m_times.at(i).direction()); |
|
143 |
||
144 |
p.setPen(Qt::white); |
|
145 |
p.drawText(50, pos, m_times.at(i).time()); |
|
146 |
p.drawText(100, pos, m_times.at(i).direction()); |
|
147 |
||
148 |
pos += 18; |
|
149 |
} |
|
150 |
} |
|
151 |
||
152 |
void MainWindow::resizeEvent(QResizeEvent*) |
|
153 |
{ |
|
154 |
QBitmap maskBitmap(width(), height()); |
|
155 |
maskBitmap.clear(); |
|
156 |
||
157 |
QPainter p(&maskBitmap); |
|
158 |
p.setBrush(Qt::black); |
|
159 |
p.drawRoundRect(0, 0, width(), height(), 20, 30); |
|
160 |
p.end(); |
|
161 |
||
162 |
setMask(maskBitmap); |
|
163 |
} |
|
164 |
||
165 |
void MainWindow::updateTimeInformation() |
|
166 |
{ |
|
167 |
m_times = TimeQuery::query(m_station.id(), m_lines, QDateTime::currentDateTime()); |
|
168 |
||
169 |
update(); |
|
170 |
} |
|
171 |
||
172 |
void MainWindow::configure() |
|
173 |
{ |
|
174 |
StationDialog dlg(m_station.name(), m_lines, this); |
|
175 |
if (dlg.exec()) { |
|
176 |
m_station = dlg.selectedStation(); |
|
177 |
m_lines = dlg.lineNumbers(); |
|
178 |
updateTimeInformation(); |
|
179 |
} |
|
180 |
} |