19 #include <QPushButton> 23 #include <QXmlInputSource> 25 #include <QGridLayout> 35 m_caption(tr(
"Tip of the Day"))
40 settings.beginGroup(
"TipOfTheDay");
42 if (!settings.value(
"showonstartup",
true).toBool())
return;
48 QGridLayout *grid =
new QGridLayout;
51 QGroupBox *groupBox =
new QGroupBox;
53 grid->addWidget(groupBox, 0, 0);
55 QGridLayout *subgrid =
new QGridLayout;
56 groupBox->setLayout(subgrid);
59 subgrid->addWidget(
m_label, 0, 0);
62 QHBoxLayout *hbox =
new QHBoxLayout;
63 grid->addLayout(hbox, 1, 0);
65 QCheckBox *show =
new QCheckBox(tr(
"Show tip on startup"));
66 hbox->addWidget(show);
71 QPushButton *prev =
new QPushButton(tr(
"<< Previous"));
72 hbox->addWidget(prev);
73 connect(prev, SIGNAL(clicked()),
this, SLOT(
previous()));
75 QPushButton *
next =
new QPushButton(tr(
"Next >>"));
76 hbox->addWidget(
next);
77 connect(
next, SIGNAL(clicked()),
this, SLOT(
next()));
79 QPushButton *close =
new QPushButton(tr(
"Close"));
80 hbox->addWidget(close);
81 connect(close, SIGNAL(clicked()),
this, SLOT(accept()));
83 close->setDefault(
true);
117 SVDEBUG <<
"TipDialog::readTips" << endl;
119 QString language = QLocale::system().name();
120 QString filename = QString(
":i18n/tips_%1.xml").arg(language);
122 if (!QFileInfo(filename).exists()) {
124 QString base = language.section(
'_', 0, 0);
125 filename = QString(
":i18n/tips_%1.xml").arg(base);
127 if (!QFileInfo(filename).exists()) {
129 filename = QString(
":i18n/tips.xml");
131 if (!QFileInfo(filename).exists())
return;
135 QFile file(filename);
137 SVDEBUG <<
"TipDialog::readTips from " << filename << endl;
139 QXmlInputSource source(&file);
142 parser.
parse(source);
156 if (++tn >=
int(
m_tips.size())) tn = 0;
159 settings.beginGroup(
"TipOfTheDay");
160 settings.setValue(
"nexttip", tn);
178 QXmlSimpleReader reader;
179 reader.setContentHandler(
this);
180 reader.setErrorHandler(
this);
181 reader.parse(source);
186 const QString &qName,
187 const QXmlAttributes &attributes)
189 QString name = qName.toLower();
191 SVDEBUG <<
"TipFileParser::startElement(" << name <<
")" << endl;
193 if (name ==
"tips") {
194 QString caption = attributes.value(
"caption");
195 SVDEBUG <<
"TipFileParser::caption = " << caption << endl;
196 if (caption !=
"") m_dialog->m_caption = caption;
197 }
else if (name ==
"tip") {
199 cerr <<
"WARNING: TipFileParser: nested <tip> elements" << endl;
202 }
else if (name ==
"text") {
205 cerr <<
"TipFileParser: adding new tip" << endl;
206 m_dialog->m_tips.push_back(
"");
208 cerr <<
"WARNING: TipFileParser: <text> outside <tip> element" << endl;
210 }
else if (name ==
"html") {
213 cerr <<
"TipFileParser: adding new tip" << endl;
214 m_dialog->m_tips.push_back(
"");
216 cerr <<
"WARNING: TipFileParser: <html> outside <tip> element" << endl;
218 }
else if (m_inHtml) {
219 m_dialog->m_tips[m_dialog->m_tips.size()-1] +=
"<" + qName;
220 for (
int i = 0; i < attributes.count(); ++i) {
221 m_dialog->m_tips[m_dialog->m_tips.size()-1] +=
222 " " + attributes.qName(i) +
"=\"" + attributes.value(i) +
"\"";
224 m_dialog->m_tips[m_dialog->m_tips.size()-1] +=
">";
227 SVDEBUG <<
"TipFileParser::startElement done" << endl;
233 const QString &qName)
235 QString name = qName.toLower();
237 if (name ==
"text") {
239 cerr <<
"WARNING: TipFileParser: </text> without <text>" << endl;
242 }
else if (name ==
"html") {
244 cerr <<
"WARNING: TipFileParser: </html> without <html>" << endl;
247 }
else if (name ==
"tip") {
249 cerr <<
"WARNING: TipFileParser: <text> without </text>" << endl;
250 }
else if (m_inHtml) {
251 cerr <<
"WARNING: TipFileParser: <html> without </html>" << endl;
252 }
else if (!m_inTip) {
253 cerr <<
"WARNING: TipFileParser: </tip> without <tip>" << endl;
256 }
else if (m_inHtml) {
257 m_dialog->m_tips[m_dialog->m_tips.size()-1] +=
"</" + qName +
">";
266 SVDEBUG <<
"TipFileParser::characters(" << text <<
")" << endl;
268 if (m_inText || m_inHtml) {
269 m_dialog->m_tips[m_dialog->m_tips.size()-1] += text;
278 QString errorString =
279 QString(
"ERROR: TipFileParser: %1 at line %2, column %3")
280 .arg(exception.message())
281 .arg(exception.lineNumber())
282 .arg(exception.columnNumber());
283 cerr << errorString << endl;
284 return QXmlDefaultHandler::error(exception);
290 QString errorString =
291 QString(
"FATAL ERROR: TipFileParser: %1 at line %2, column %3")
292 .arg(exception.message())
293 .arg(exception.lineNumber())
294 .arg(exception.columnNumber());
295 cerr << errorString << endl;
296 return QXmlDefaultHandler::fatalError(exception);
TipFileParser(TipDialog *dialog)
virtual bool endElement(const QString &namespaceURI, const QString &localName, const QString &qName)
std::vector< QString > m_tips
void parse(QXmlInputSource &source)
TipDialog(QWidget *parent=0)
bool error(const QXmlParseException &exception)
virtual bool startElement(const QString &namespaceURI, const QString &localName, const QString &qName, const QXmlAttributes &atts)
bool fatalError(const QXmlParseException &exception)
virtual bool characters(const QString &)