| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2009 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:LGPL$
|
|---|
| 10 | ** Commercial Usage
|
|---|
| 11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
|---|
| 12 | ** accordance with the Qt Commercial License Agreement provided with the
|
|---|
| 13 | ** Software or, alternatively, in accordance with the terms contained in
|
|---|
| 14 | ** a written agreement between you and Nokia.
|
|---|
| 15 | **
|
|---|
| 16 | ** GNU Lesser General Public License Usage
|
|---|
| 17 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
|---|
| 18 | ** General Public License version 2.1 as published by the Free Software
|
|---|
| 19 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
|---|
| 20 | ** packaging of this file. Please review the following information to
|
|---|
| 21 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
|---|
| 22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|---|
| 23 | **
|
|---|
| 24 | ** In addition, as a special exception, Nokia gives you certain additional
|
|---|
| 25 | ** rights. These rights are described in the Nokia Qt LGPL Exception
|
|---|
| 26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|---|
| 27 | **
|
|---|
| 28 | ** GNU General Public License Usage
|
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU
|
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software
|
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
|---|
| 32 | ** packaging of this file. Please review the following information to
|
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
|---|
| 35 | **
|
|---|
| 36 | ** If you have questions regarding the use of this file, please contact
|
|---|
| 37 | ** Nokia at qt-info@nokia.com.
|
|---|
| 38 | ** $QT_END_LICENSE$
|
|---|
| 39 | **
|
|---|
| 40 | ****************************************************************************/
|
|---|
| 41 |
|
|---|
| 42 | #include "mainwindow.h"
|
|---|
| 43 | #include "stationdialog.h"
|
|---|
| 44 |
|
|---|
| 45 | #include <QtCore/QSettings>
|
|---|
| 46 | #include <QtCore/QTimer>
|
|---|
| 47 | #include <QtGui/QAction>
|
|---|
| 48 | #include <QtGui/QApplication>
|
|---|
| 49 | #include <QtGui/QBitmap>
|
|---|
| 50 | #include <QtGui/QLinearGradient>
|
|---|
| 51 | #include <QtGui/QMouseEvent>
|
|---|
| 52 | #include <QtGui/QPainter>
|
|---|
| 53 |
|
|---|
| 54 | MainWindow::MainWindow()
|
|---|
| 55 | : QWidget(0, Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint)
|
|---|
| 56 | {
|
|---|
| 57 | QAction *quitAction = new QAction(tr("E&xit"), this);
|
|---|
| 58 | quitAction->setShortcuts(QKeySequence::Quit);
|
|---|
| 59 | connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
|
|---|
| 60 |
|
|---|
| 61 | QAction *configAction = new QAction(tr("&Select station..."), this);
|
|---|
| 62 | configAction->setShortcut(tr("Ctrl+C"));
|
|---|
| 63 | connect(configAction, SIGNAL(triggered()), this, SLOT(configure()));
|
|---|
| 64 |
|
|---|
| 65 | addAction(configAction);
|
|---|
| 66 | addAction(quitAction);
|
|---|
| 67 |
|
|---|
| 68 | setContextMenuPolicy(Qt::ActionsContextMenu);
|
|---|
| 69 |
|
|---|
| 70 | setWindowTitle(tr("Traffic Info Oslo"));
|
|---|
| 71 |
|
|---|
| 72 | const QSettings settings("Qt Traffic Info", "trafficinfo");
|
|---|
| 73 | m_station = StationInformation(settings.value("stationId", "03012130").toString(),
|
|---|
| 74 | settings.value("stationName", "Nydalen [T-bane] (OSL)").toString());
|
|---|
| 75 | m_lines = settings.value("lines", QStringList()).toStringList();
|
|---|
| 76 |
|
|---|
| 77 | QTimer *timer = new QTimer(this);
|
|---|
| 78 | connect(timer, SIGNAL(timeout()), this, SLOT(updateTimeInformation()));
|
|---|
| 79 | timer->start(1000*60*5);
|
|---|
| 80 | QMetaObject::invokeMethod(this, SLOT(updateTimeInformation()), Qt::QueuedConnection);
|
|---|
| 81 | }
|
|---|
| 82 |
|
|---|
| 83 | MainWindow::~MainWindow()
|
|---|
| 84 | {
|
|---|
| 85 | QSettings settings("Qt Traffic Info", "trafficinfo");
|
|---|
| 86 | settings.setValue("stationId", m_station.id());
|
|---|
| 87 | settings.setValue("stationName", m_station.name());
|
|---|
| 88 | settings.setValue("lines", m_lines);
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | QSize MainWindow::sizeHint() const
|
|---|
| 92 | {
|
|---|
| 93 | return QSize(300, 200);
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | void MainWindow::mouseMoveEvent(QMouseEvent *event)
|
|---|
| 97 | {
|
|---|
| 98 | if (event->buttons() & Qt::LeftButton) {
|
|---|
| 99 | move(event->globalPos() - m_dragPosition);
|
|---|
| 100 | event->accept();
|
|---|
| 101 | }
|
|---|
| 102 | }
|
|---|
| 103 |
|
|---|
| 104 | void MainWindow::mousePressEvent(QMouseEvent *event)
|
|---|
| 105 | {
|
|---|
| 106 | if (event->button() == Qt::LeftButton) {
|
|---|
| 107 | m_dragPosition = event->globalPos() - frameGeometry().topLeft();
|
|---|
| 108 | event->accept();
|
|---|
| 109 | }
|
|---|
| 110 | }
|
|---|
| 111 |
|
|---|
| 112 | void MainWindow::paintEvent(QPaintEvent*)
|
|---|
| 113 | {
|
|---|
| 114 | const QPoint start(width()/2, 0);
|
|---|
| 115 | const QPoint finalStop(width()/2, height());
|
|---|
| 116 | QLinearGradient gradient(start, finalStop);
|
|---|
| 117 | const QColor qtGreen(102, 176, 54);
|
|---|
| 118 | gradient.setColorAt(0, qtGreen.dark());
|
|---|
| 119 | gradient.setColorAt(0.5, qtGreen);
|
|---|
| 120 | gradient.setColorAt(1, qtGreen.dark());
|
|---|
| 121 |
|
|---|
| 122 | QPainter p(this);
|
|---|
| 123 | p.fillRect(0, 0, width(), height(), gradient);
|
|---|
| 124 |
|
|---|
| 125 | QFont headerFont("Sans Serif", 12, QFont::Bold);
|
|---|
| 126 | QFont normalFont("Sans Serif", 9, QFont::Normal);
|
|---|
| 127 |
|
|---|
| 128 | // draw it twice for shadow effect
|
|---|
| 129 | p.setFont(headerFont);
|
|---|
| 130 | QRect headerRect(1, 1, width(), 25);
|
|---|
| 131 | p.setPen(Qt::black);
|
|---|
| 132 | p.drawText(headerRect, Qt::AlignCenter, m_station.name());
|
|---|
| 133 |
|
|---|
| 134 | headerRect.moveTopLeft(QPoint(0, 0));
|
|---|
| 135 | p.setPen(Qt::white);
|
|---|
| 136 | p.drawText(headerRect, Qt::AlignCenter, m_station.name());
|
|---|
| 137 |
|
|---|
| 138 | p.setFont(normalFont);
|
|---|
| 139 | int pos = 40;
|
|---|
| 140 | for (int i = 0; i < m_times.count() && i < 9; ++i) {
|
|---|
| 141 | p.setPen(Qt::black);
|
|---|
| 142 | p.drawText(51, pos + 1, m_times.at(i).time());
|
|---|
| 143 | p.drawText(101, pos + 1, m_times.at(i).direction());
|
|---|
| 144 |
|
|---|
| 145 | p.setPen(Qt::white);
|
|---|
| 146 | p.drawText(50, pos, m_times.at(i).time());
|
|---|
| 147 | p.drawText(100, pos, m_times.at(i).direction());
|
|---|
| 148 |
|
|---|
| 149 | pos += 18;
|
|---|
| 150 | }
|
|---|
| 151 | }
|
|---|
| 152 |
|
|---|
| 153 | void MainWindow::resizeEvent(QResizeEvent*)
|
|---|
| 154 | {
|
|---|
| 155 | QBitmap maskBitmap(width(), height());
|
|---|
| 156 | maskBitmap.clear();
|
|---|
| 157 |
|
|---|
| 158 | QPainter p(&maskBitmap);
|
|---|
| 159 | p.setBrush(Qt::black);
|
|---|
| 160 | p.drawRoundRect(0, 0, width(), height(), 20, 30);
|
|---|
| 161 | p.end();
|
|---|
| 162 |
|
|---|
| 163 | setMask(maskBitmap);
|
|---|
| 164 | }
|
|---|
| 165 |
|
|---|
| 166 | void MainWindow::updateTimeInformation()
|
|---|
| 167 | {
|
|---|
| 168 | m_times = TimeQuery::query(m_station.id(), m_lines, QDateTime::currentDateTime());
|
|---|
| 169 |
|
|---|
| 170 | update();
|
|---|
| 171 | }
|
|---|
| 172 |
|
|---|
| 173 | void MainWindow::configure()
|
|---|
| 174 | {
|
|---|
| 175 | StationDialog dlg(m_station.name(), m_lines, this);
|
|---|
| 176 | if (dlg.exec()) {
|
|---|
| 177 | m_station = dlg.selectedStation();
|
|---|
| 178 | m_lines = dlg.lineNumbers();
|
|---|
| 179 | updateTimeInformation();
|
|---|
| 180 | }
|
|---|
| 181 | }
|
|---|