1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
|
/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
/*
Sonic Visualiser
An audio file viewer and annotation editor.
Centre for Digital Music, Queen Mary, University of London.
This file copyright 2007 QMUL.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version. See the file
COPYING included with this distribution for more information.
*/
#include "TipDialog.h"
#include <QLabel>
#include <QPushButton>
#include <QSettings>
#include <QLabel>
#include <QLocale>
#include <QXmlInputSource>
#include <QFileInfo>
#include <QGridLayout>
#include <QGroupBox>
#include <QCheckBox>
#include <iostream>
namespace sv {
TipDialog::TipDialog(QWidget *parent) :
QDialog(parent),
m_tipNumber(0),
m_label(nullptr),
m_caption(tr("Tip of the Day"))
{
readTips();
QSettings settings;
settings.beginGroup("TipOfTheDay");
if (!settings.value("showonstartup", true).toBool()) return;
m_tipNumber = settings.value("nexttip", 0).toInt();
setWindowTitle(m_caption);
QGridLayout *grid = new QGridLayout;
setLayout(grid);
QGroupBox *groupBox = new QGroupBox;
// groupBox->setTitle(m_caption);
grid->addWidget(groupBox, 0, 0);
QGridLayout *subgrid = new QGridLayout;
groupBox->setLayout(subgrid);
m_label = new QLabel;
subgrid->addWidget(m_label, 0, 0);
m_label->setWordWrap(true);
QHBoxLayout *hbox = new QHBoxLayout;
grid->addLayout(hbox, 1, 0);
QCheckBox *show = new QCheckBox(tr("Show tip on startup"));
hbox->addWidget(show);
hbox->addSpacing(20);
hbox->addStretch(10);
QPushButton *prev = new QPushButton(tr("<< Previous"));
hbox->addWidget(prev);
connect(prev, SIGNAL(clicked()), this, SLOT(previous()));
QPushButton *next = new QPushButton(tr("Next >>"));
hbox->addWidget(next);
connect(next, SIGNAL(clicked()), this, SLOT(next()));
QPushButton *close = new QPushButton(tr("Close"));
hbox->addWidget(close);
connect(close, SIGNAL(clicked()), this, SLOT(accept()));
close->setDefault(true);
showTip();
}
TipDialog::~TipDialog()
{
}
void
TipDialog::next()
{
if (++m_tipNumber >= int(m_tips.size())) {
//!!! The tips file should define where we loop back to -- the
// first one at least is likely to be a generic welcome message
m_tipNumber = 0;
}
showTip();
}
void
TipDialog::previous()
{
if (--m_tipNumber < 0) {
m_tipNumber = int(m_tips.size()) - 1;
}
showTip();
}
void
TipDialog::readTips()
{
SVDEBUG << "TipDialog::readTips" << endl;
QString language = QLocale::system().name();
QString filename = QString(":i18n/tips_%1.xml").arg(language);
if (!QFileInfo(filename).exists()) {
QString base = language.section('_', 0, 0);
filename = QString(":i18n/tips_%1.xml").arg(base);
if (!QFileInfo(filename).exists()) {
filename = QString(":i18n/tips.xml");
if (!QFileInfo(filename).exists()) return;
}
}
QFile file(filename);
SVDEBUG << "TipDialog::readTips from " << filename << endl;
QXmlInputSource source(&file);
TipFileParser parser(this);
parser.parse(source);
}
void
TipDialog::showTip()
{
if (m_tipNumber < int(m_tips.size())) {
SVCERR << "Tip " << m_tipNumber << " is: " << m_tips[m_tipNumber] << endl;
m_label->setText(m_tips[m_tipNumber]);
} else {
accept();
}
int tn = m_tipNumber;
if (++tn >= int(m_tips.size())) tn = 0; //!!! as above
QSettings settings;
settings.beginGroup("TipOfTheDay");
settings.setValue("nexttip", tn);
}
TipDialog::TipFileParser::TipFileParser(TipDialog *dialog) :
m_dialog(dialog),
m_inTip(false),
m_inText(false),
m_inHtml(false)
{
}
TipDialog::TipFileParser::~TipFileParser()
{
}
void
TipDialog::TipFileParser::parse(QXmlInputSource &source)
{
QXmlSimpleReader reader;
reader.setContentHandler(this);
reader.setErrorHandler(this);
reader.parse(source);
}
bool
TipDialog::TipFileParser::startElement(const QString &, const QString &,
const QString &qName,
const QXmlAttributes &attributes)
{
QString name = qName.toLower();
SVDEBUG << "TipFileParser::startElement(" << name << ")" << endl;
if (name == "tips") {
QString caption = attributes.value("caption");
SVDEBUG << "TipFileParser::caption = " << caption << endl;
if (caption != "") m_dialog->m_caption = caption;
} else if (name == "tip") {
if (m_inTip) {
SVCERR << "WARNING: TipFileParser: nested <tip> elements" << endl;
}
m_inTip = true;
} else if (name == "text") {
if (m_inTip) {
m_inText = true;
SVCERR << "TipFileParser: adding new tip" << endl;
m_dialog->m_tips.push_back("");
} else {
SVCERR << "WARNING: TipFileParser: <text> outside <tip> element" << endl;
}
} else if (name == "html") {
if (m_inTip) {
m_inHtml = true;
SVCERR << "TipFileParser: adding new tip" << endl;
m_dialog->m_tips.push_back("");
} else {
SVCERR << "WARNING: TipFileParser: <html> outside <tip> element" << endl;
}
} else if (m_inHtml) {
m_dialog->m_tips[m_dialog->m_tips.size()-1] += "<" + qName;
for (int i = 0; i < attributes.count(); ++i) {
m_dialog->m_tips[m_dialog->m_tips.size()-1] +=
" " + attributes.qName(i) + "=\"" + attributes.value(i) + "\"";
}
m_dialog->m_tips[m_dialog->m_tips.size()-1] += ">";
}
SVDEBUG << "TipFileParser::startElement done" << endl;
return true;
}
bool
TipDialog::TipFileParser::endElement(const QString &, const QString &,
const QString &qName)
{
QString name = qName.toLower();
if (name == "text") {
if (!m_inText) {
SVCERR << "WARNING: TipFileParser: </text> without <text>" << endl;
}
m_inText = false;
} else if (name == "html") {
if (!m_inHtml) {
SVCERR << "WARNING: TipFileParser: </html> without <html>" << endl;
}
m_inHtml = false;
} else if (name == "tip") {
if (m_inText) {
SVCERR << "WARNING: TipFileParser: <text> without </text>" << endl;
} else if (m_inHtml) {
SVCERR << "WARNING: TipFileParser: <html> without </html>" << endl;
} else if (!m_inTip) {
SVCERR << "WARNING: TipFileParser: </tip> without <tip>" << endl;
}
m_inTip = false;
} else if (m_inHtml) {
m_dialog->m_tips[m_dialog->m_tips.size()-1] += "</" + qName + ">";
}
return true;
}
bool
TipDialog::TipFileParser::characters(const QString &text)
{
SVDEBUG << "TipFileParser::characters(" << text << ")" << endl;
if (m_inText || m_inHtml) {
m_dialog->m_tips[m_dialog->m_tips.size()-1] += text;
}
return true;
}
bool
TipDialog::TipFileParser::error(const QXmlParseException &exception)
{
QString errorString =
QString("ERROR: TipFileParser: %1 at line %2, column %3")
.arg(exception.message())
.arg(exception.lineNumber())
.arg(exception.columnNumber());
SVCERR << errorString << endl;
return QXmlDefaultHandler::error(exception);
}
bool
TipDialog::TipFileParser::fatalError(const QXmlParseException &exception)
{
QString errorString =
QString("FATAL ERROR: TipFileParser: %1 at line %2, column %3")
.arg(exception.message())
.arg(exception.lineNumber())
.arg(exception.columnNumber());
SVCERR << errorString << endl;
return QXmlDefaultHandler::fatalError(exception);
}
} // end namespace sv
|