comparison widgets/TipDialog.cpp @ 225:6f46179086c0

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