Initial import from the monolithic Qt.
[profile/ivi/qtxmlpatterns.git] / examples / xmlpatterns / trafficinfo / mainwindow.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
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 **
9 ** $QT_BEGIN_LICENSE:BSD$
10 ** You may use this file under the terms of the BSD license as follows:
11 **
12 ** "Redistribution and use in source and binary forms, with or without
13 ** modification, are permitted provided that the following conditions are
14 ** met:
15 **   * Redistributions of source code must retain the above copyright
16 **     notice, this list of conditions and the following disclaimer.
17 **   * Redistributions in binary form must reproduce the above copyright
18 **     notice, this list of conditions and the following disclaimer in
19 **     the documentation and/or other materials provided with the
20 **     distribution.
21 **   * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
22 **     the names of its contributors may be used to endorse or promote
23 **     products derived from this software without specific prior written
24 **     permission.
25 **
26 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
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 }