Advanced Qt Programming by Mark Summerfield
ISBN: 978-0321635907
weathertrayicon.cpp
[..]
void WeatherTrayIcon::createContextMenu()
{
QStringList airports;
airports << "Austin-Bergstrom International Airport (KAUS)"
<< "Chicago/Ohare (KORD)"
<< "Dallas / Fort Worth International Airport (KDFW)"
<< "Detroit City Airport (KDET)"
<< "Houston Intercontinental Airport (KIAH)"
<< "Indianapolis International Airport (KIND)"
<< "Jacksonville International Airport (KJAX)"
<< "Los Angeles Intl Airport (KLAX)"
<< "New York/John F. Kennedy Intl Airport (KJFK)"
<< "Philadelphia International Airport (KPHL)"
<< "Phoenix/Sky Harbor (KPHX)"
<< "Port Columbus International Airport (KCMH)"
<< "San Antonio International Airport (KSAT)"
<< "San Diego/Brown Fld (KSDM)"
<< "San Francisco Intl Airport (KSFO)"
<< "San Jose International Airport (KSJC)";
QSettings settings;
airport = settings.value("airport", QVariant(airports.at(0)))
.toString();
QActionGroup *group = new QActionGroup(this);
foreach (const QString &anAirport, airports) {
QAction *action = menu.addAction(anAirport);
group->addAction(action);
action->setCheckable(true);
action->setChecked(anAirport == airport);
action->setData(anAirport);
}
connect(group, SIGNAL(triggered(QAction*)),
this, SLOT(setAirport(QAction*)));
menu.addSeparator();
menu.addAction(QIcon(":/exit.png"), tr("E&xit"), qApp,
SLOT(quit()));
AQP::accelerateMenu(&menu);
setContextMenu(&menu);
}
void WeatherTrayIcon::requestXml()
{
#ifdef NO_REGEX
QString airportId = airport.right(6);
if (airportId.startsWith("(") && airportId.endsWith(")")) {
QString url = QString("http://www.weather.gov/xml/"
"current_obs/%1.xml").arg(airportId.mid(1, 4));
networkXmlAccess->get(QNetworkRequest(QUrl(url)));
}
#else
QRegExp airportIdRx("^.*[(]([A-Z]{4})[)]$");
airportIdRx.setPatternSyntax(QRegExp::RegExp2);
if (airportIdRx.exactMatch(airport)) {
QString url = QString("http://www.weather.gov/xml/"
"current_obs/%1.xml").arg(airportIdRx.cap(1));
networkXmlAccess->get(QNetworkRequest(QUrl(url)));
}
#endif
setToolTip(tr("Failed to connect to network.\n"
"A proxy can be set on the command line:\n"
"weathertrayicon --help"));
}
[..]