1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: http://www.qt-project.org/
7 ** This file is part of the examples of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:BSD$
10 ** You may use this file under the terms of the BSD license as follows:
12 ** "Redistribution and use in source and binary forms, with or without
13 ** modification, are permitted provided that the following conditions are
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
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
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."
39 ****************************************************************************/
41 #include "mainwindow.h"
42 #include "stationdialog.h"
44 #include <QtCore/QSettings>
45 #include <QtCore/QTimer>
46 #include <QtWidgets/QAction>
47 #include <QtWidgets/QApplication>
48 #include <QtGui/QBitmap>
49 #include <QtGui/QLinearGradient>
50 #include <QtGui/QMouseEvent>
51 #include <QtGui/QPainter>
53 MainWindow::MainWindow()
54 : QWidget(0, Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint)
56 QAction *quitAction = new QAction(tr("E&xit"), this);
57 quitAction->setShortcuts(QKeySequence::Quit);
58 connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
60 QAction *configAction = new QAction(tr("&Select station..."), this);
61 configAction->setShortcut(tr("Ctrl+C"));
62 connect(configAction, SIGNAL(triggered()), this, SLOT(configure()));
64 addAction(configAction);
65 addAction(quitAction);
67 setContextMenuPolicy(Qt::ActionsContextMenu);
69 setWindowTitle(tr("Traffic Info Oslo"));
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();
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);
82 MainWindow::~MainWindow()
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);
90 QSize MainWindow::sizeHint() const
92 return QSize(300, 200);
95 void MainWindow::mouseMoveEvent(QMouseEvent *event)
97 if (event->buttons() & Qt::LeftButton) {
98 move(event->globalPos() - m_dragPosition);
103 void MainWindow::mousePressEvent(QMouseEvent *event)
105 if (event->button() == Qt::LeftButton) {
106 m_dragPosition = event->globalPos() - frameGeometry().topLeft();
111 void MainWindow::paintEvent(QPaintEvent*)
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());
122 p.fillRect(0, 0, width(), height(), gradient);
124 QFont headerFont("Sans Serif", 12, QFont::Bold);
125 QFont normalFont("Sans Serif", 9, QFont::Normal);
127 // draw it twice for shadow effect
128 p.setFont(headerFont);
129 QRect headerRect(1, 1, width(), 25);
131 p.drawText(headerRect, Qt::AlignCenter, m_station.name());
133 headerRect.moveTopLeft(QPoint(0, 0));
135 p.drawText(headerRect, Qt::AlignCenter, m_station.name());
137 p.setFont(normalFont);
139 for (int i = 0; i < m_times.count() && i < 9; ++i) {
141 p.drawText(51, pos + 1, m_times.at(i).time());
142 p.drawText(101, pos + 1, m_times.at(i).direction());
145 p.drawText(50, pos, m_times.at(i).time());
146 p.drawText(100, pos, m_times.at(i).direction());
152 void MainWindow::resizeEvent(QResizeEvent*)
154 QBitmap maskBitmap(width(), height());
157 QPainter p(&maskBitmap);
158 p.setBrush(Qt::black);
159 p.drawRoundRect(0, 0, width(), height(), 20, 30);
165 void MainWindow::updateTimeInformation()
167 m_times = TimeQuery::query(m_station.id(), m_lines, QDateTime::currentDateTime());
172 void MainWindow::configure()
174 StationDialog dlg(m_station.name(), m_lines, this);
176 m_station = dlg.selectedStation();
177 m_lines = dlg.lineNumbers();
178 updateTimeInformation();