| 1 | /**************************************************************************** | 
|---|
| 2 | ** | 
|---|
| 3 | ** Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies). | 
|---|
| 4 | ** Contact: Qt Software Information (qt-info@nokia.com) | 
|---|
| 5 | ** | 
|---|
| 6 | ** This file is part of the examples of the Qt Toolkit. | 
|---|
| 7 | ** | 
|---|
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ | 
|---|
| 9 | ** Commercial Usage | 
|---|
| 10 | ** Licensees holding valid Qt Commercial licenses may use this file in | 
|---|
| 11 | ** accordance with the Qt Commercial License Agreement provided with the | 
|---|
| 12 | ** Software or, alternatively, in accordance with the terms contained in | 
|---|
| 13 | ** a written agreement between you and Nokia. | 
|---|
| 14 | ** | 
|---|
| 15 | ** GNU Lesser General Public License Usage | 
|---|
| 16 | ** Alternatively, this file may be used under the terms of the GNU Lesser | 
|---|
| 17 | ** General Public License version 2.1 as published by the Free Software | 
|---|
| 18 | ** Foundation and appearing in the file LICENSE.LGPL included in the | 
|---|
| 19 | ** packaging of this file.  Please review the following information to | 
|---|
| 20 | ** ensure the GNU Lesser General Public License version 2.1 requirements | 
|---|
| 21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | 
|---|
| 22 | ** | 
|---|
| 23 | ** In addition, as a special exception, Nokia gives you certain | 
|---|
| 24 | ** additional rights. These rights are described in the Nokia Qt LGPL | 
|---|
| 25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this | 
|---|
| 26 | ** 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 are unsure which license is appropriate for your use, please | 
|---|
| 37 | ** contact the sales department at qt-sales@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->setShortcut(tr("Ctrl+Q")); | 
|---|
| 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 | QTimer *timer = new QTimer(this); | 
|---|
| 73 | connect(timer, SIGNAL(timeout()), this, SLOT(updateTimeInformation())); | 
|---|
| 74 | timer->start(1000*60*5); | 
|---|
| 75 |  | 
|---|
| 76 | const QSettings settings("Qt Software", "trafficinfo"); | 
|---|
| 77 | m_station = StationInformation(settings.value("stationId", "03012130").toString(), | 
|---|
| 78 | settings.value("stationName", "Nydalen [T-bane] (OSL)").toString()); | 
|---|
| 79 | m_lines = settings.value("lines", QStringList()).toStringList(); | 
|---|
| 80 |  | 
|---|
| 81 | updateTimeInformation(); | 
|---|
| 82 | } | 
|---|
| 83 |  | 
|---|
| 84 | MainWindow::~MainWindow() | 
|---|
| 85 | { | 
|---|
| 86 | QSettings settings("Qt Software", "trafficinfo"); | 
|---|
| 87 | settings.setValue("stationId", m_station.id()); | 
|---|
| 88 | settings.setValue("stationName", m_station.name()); | 
|---|
| 89 | settings.setValue("lines", m_lines); | 
|---|
| 90 | } | 
|---|
| 91 |  | 
|---|
| 92 | QSize MainWindow::sizeHint() const | 
|---|
| 93 | { | 
|---|
| 94 | return QSize(300, 200); | 
|---|
| 95 | } | 
|---|
| 96 |  | 
|---|
| 97 | void MainWindow::mouseMoveEvent(QMouseEvent *event) | 
|---|
| 98 | { | 
|---|
| 99 | if (event->buttons() & Qt::LeftButton) { | 
|---|
| 100 | move(event->globalPos() - m_dragPosition); | 
|---|
| 101 | event->accept(); | 
|---|
| 102 | } | 
|---|
| 103 | } | 
|---|
| 104 |  | 
|---|
| 105 | void MainWindow::mousePressEvent(QMouseEvent *event) | 
|---|
| 106 | { | 
|---|
| 107 | if (event->button() == Qt::LeftButton) { | 
|---|
| 108 | m_dragPosition = event->globalPos() - frameGeometry().topLeft(); | 
|---|
| 109 | event->accept(); | 
|---|
| 110 | } | 
|---|
| 111 | } | 
|---|
| 112 |  | 
|---|
| 113 | void MainWindow::paintEvent(QPaintEvent*) | 
|---|
| 114 | { | 
|---|
| 115 | QLinearGradient gradient(QPoint(width()/2, 0), QPoint(width()/2, height())); | 
|---|
| 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 | TimeQuery query; | 
|---|
| 168 | m_times = query.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 | } | 
|---|