Subversion Repositories Scribus

Rev

Rev 24665 | Rev 24729 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
4430 cbradney 1
/*
2
For general Scribus (>=1.3.2) copyright and licensing information please refer
3
to the COPYING file provided with the program. Following this notice may exist
4
a copyright and/or license notice that predates the release of Scribus 1.3.2
5
for which a new license (GPL+exception) is in place.
6
*/
564 cbradney 7
/***************************************************************************
8
*   Copyright (C) 2004 by Craig Bradney                                   *
9
*   cbradney@zip.com.au                                                   *
2052 subik 10
*   Copyright (C) 2005 by Petr Vanek                                      *
11
*   petr@yarpen.cz                                                        *
564 cbradney 12
*                                                                         *
13
*   This program is free software; you can redistribute it and/or modify  *
14
*   it under the terms of the GNU General Public License as published by  *
15
*   the Free Software Foundation; either version 2 of the License, or     *
16
*   (at your option) any later version.                                   *
17
*                                                                         *
18
*   This program is distributed in the hope that it will be useful,       *
19
*   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
20
*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
21
*   GNU General Public License for more details.                          *
22
*                                                                         *
23
*   You should have received a copy of the GNU General Public License     *
24
*   along with this program; if not, write to the                         *
25
*   Free Software Foundation, Inc.,                                       *
18122 mrdocs 26
*   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.             *
564 cbradney 27
***************************************************************************/
560 cbradney 28
 
3 paul 29
#include "helpbrowser.h"
13530 pierre 30
#include "ui/helpnavigation.h"
560 cbradney 31
 
10108 cbradney 32
#include <QAction>
11645 fschmid 33
// #include <QDebug>
10108 cbradney 34
#include <QDir>
13530 pierre 35
#include <QDockWidget>
10108 cbradney 36
#include <QDomDocument>
10859 cbradney 37
#include <QEvent>
10006 cbradney 38
#include <QFileDialog>
10108 cbradney 39
#include <QFileInfo>
40
#include <QHeaderView>
41
#include <QInputDialog>
42
#include <QItemSelectionModel>
43
#include <QList>
44
#include <QMessageBox>
45
#include <QModelIndex>
46
#include <QModelIndexList>
47
#include <QPainter>
48
#include <QPrinter>
10511 fschmid 49
#include <QPrintDialog>
10006 cbradney 50
#include <QProcess>
10108 cbradney 51
#include <QPushButton>
52
#include <QString>
53
#include <QStandardItem>
54
#include <QTextEdit>
55
#include <QTreeView>
23219 jghali 56
#include <QVector>
24229 jghali 57
#include <QXmlStreamReader>
10108 cbradney 58
 
59
#include "prefsmanager.h"
10369 cbradney 60
#include "scribuscore.h"
19271 craig 61
#include "util_debug.h"
20185 craig 62
#include "iconmanager.h"
20227 craig 63
#include "util.h"
128 Franz 64
 
24665 jghali 65
/*! \brief XML parsed for documentation history.
2048 subik 66
This is small helper class which reads saved bookmarks configuration
10108 cbradney 67
from ~/.scribus/doc/history.xml file.
68
The reference to historyBrowser is a reference to the dialog.
2048 subik 69
\author Petr Vanek <petr@yarpen.cz>
70
*/
24229 jghali 71
class HistoryParser2
2048 subik 72
{
73
        public:
24229 jghali 74
                HelpBrowser *helpBrowser { nullptr };
2048 subik 75
 
24229 jghali 76
                bool parse(const QString& fileName)
2048 subik 77
                {
24229 jghali 78
                        QFile qFile(fileName);
79
                        if (!qFile.open(QFile::ReadOnly))
80
                                return false;
2048 subik 81
 
24229 jghali 82
                        QXmlStreamReader sReader(&qFile);
83
                        QXmlStreamReader::TokenType tagType;
84
                        QStringRef tagName;
85
 
86
                        while (!sReader.atEnd() && !sReader.hasError())
2048 subik 87
                        {
24229 jghali 88
                                tagType = sReader.readNext();
89
                                if (tagType != QXmlStreamReader::StartElement)
90
                                        continue;
91
 
92
                                tagName = sReader.name();
93
                                if (tagName != "item")
94
                                        continue;
95
 
10108 cbradney 96
                                struct histd2 his;
24229 jghali 97
                                QXmlStreamAttributes attrs = sReader.attributes();
98
                                his.title = attrs.value(QLatin1String("title")).toString();
99
                                his.url = attrs.value(QLatin1String("url")).toString();
23223 jghali 100
                                helpBrowser->addHistoryItem(his);
2048 subik 101
                        }
24229 jghali 102
                        qFile.close();
2048 subik 103
 
24229 jghali 104
                        return (!sReader.hasError());
2048 subik 105
                }
106
};
107
 
24665 jghali 108
/*! \brief XML parsed for documentation bookmarks.
2052 subik 109
This is small helper class which reads saved bookmarks configuration
10108 cbradney 110
from ~/.scribus/doc/bookmarks.xml file.
111
The reference to QListView *view is a reference to the list view with bookmarks
2052 subik 112
\author Petr Vanek <petr@yarpen.cz>
113
*/
24229 jghali 114
class BookmarkParser2
2052 subik 115
{
116
        public:
24229 jghali 117
                QTreeWidget* view { nullptr };
118
                QMap<QString, QString>* quickHelpIndex { nullptr };
119
                QMap<QString, QPair<QString, QString> >* bookmarkIndex { nullptr };
2048 subik 120
 
24229 jghali 121
                bool parse(const QString& fileName)
2052 subik 122
                {
24229 jghali 123
                        QFile qFile(fileName);
124
                        if (!qFile.open(QFile::ReadOnly))
125
                                return false;
2052 subik 126
 
24229 jghali 127
                        QXmlStreamReader sReader(&qFile);
128
                        QXmlStreamReader::TokenType tagType;
129
                        QStringRef tagName;
130
 
131
                        while (!sReader.atEnd() && !sReader.hasError())
2052 subik 132
                        {
24229 jghali 133
                                tagType = sReader.readNext();
134
                                if (tagType != QXmlStreamReader::StartElement)
135
                                        continue;
136
 
137
                                tagName = sReader.name();
138
                                if (tagName != "item")
139
                                        continue;
140
 
141
                                QXmlStreamAttributes attrs = sReader.attributes();
142
                                QString title = attrs.value(QLatin1String("title")).toString();
143
                                QString pageTitle = attrs.value(QLatin1String("pagetitle")).toString();
144
                                QString pageUrl = attrs.value(QLatin1String("url")).toString();
145
 
10676 cbradney 146
                                //TODO : This will dump items if bookmarks get loaded into a different GUI language
24229 jghali 147
                                if (quickHelpIndex->contains(pageTitle))
10108 cbradney 148
                                {
24229 jghali 149
                                        bookmarkIndex->insert(title, qMakePair(pageTitle, pageUrl));
150
                                        view->addTopLevelItem(new QTreeWidgetItem(view, QStringList() << title));
10108 cbradney 151
                                }
2052 subik 152
                        }
24229 jghali 153
                        qFile.close();
2052 subik 154
 
24229 jghali 155
                        return (!sReader.hasError());
2052 subik 156
                }
157
};
158
 
20791 jghali 159
bool HelpBrowser::firstRun = true;
10887 cbradney 160
 
10108 cbradney 161
HelpBrowser::HelpBrowser( QWidget* parent, const QString& /*caption*/, const QString& guiLanguage, const QString& jumpToSection, const QString& jumpToFile)
162
        : QMainWindow( parent )
163
{
23206 jghali 164
        firstRun = true;
10108 cbradney 165
        setupUi(this);
13530 pierre 166
        helpNav = new HelpNavigation(this);
18204 fschmid 167
#if defined(Q_OS_MAC) && defined(HELP_NAV_AS_DRAWER)
13530 pierre 168
        // TODO
169
#else
170
        QDockWidget * dckw = new QDockWidget(tr("Navigation"),this);
171
        dckw->setWidget(helpNav);
172
        addDockWidget(Qt::LeftDockWidgetArea, dckw);
173
#endif
174
        progressBar = new QProgressBar(this);
175
        progressBar->setRange(0,100);
176
        statusBar()->addPermanentWidget(progressBar);
177
 
10108 cbradney 178
        setupLocalUI();
23223 jghali 179
        m_language = guiLanguage.isEmpty() ? QString("en") : guiLanguage.left(2);
180
        m_defaultBaseDir = ScPaths::instance().docDir() + "en/"; //Sane default for help location
181
        m_finalBaseDir = ScPaths::instance().docDir() + "en/"; //Sane default for help location
182
        textBrowser->setSearchPaths(QStringList(m_finalBaseDir));
23219 jghali 183
 
10108 cbradney 184
        loadMenu();
23223 jghali 185
        if (m_menuModel != nullptr)
10676 cbradney 186
        {
187
                readBookmarks();
188
                readHistory();
189
                jumpToHelpSection(jumpToSection, jumpToFile );
190
                languageChange();
191
        }
192
        else
193
        {
194
                displayNoHelp();
195
        }
10108 cbradney 196
}
197
 
198
HelpBrowser::~HelpBrowser()
199
{
23206 jghali 200
        firstRun = true;
10108 cbradney 201
}
202
 
203
void HelpBrowser::closeEvent(QCloseEvent * event)
204
{
23223 jghali 205
        delete m_menuModel;
10108 cbradney 206
 
207
        // no need to delete child widgets, Qt does it all for us
208
        // bookmarks
209
        QFile bookFile(bookmarkFile());
210
        if (bookFile.open(QIODevice::WriteOnly))
4519 subik 211
        {
10108 cbradney 212
                QTextStream stream(&bookFile);
213
                stream.setCodec("UTF-8");
214
                stream << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
215
                stream << "<bookmarks>\n";
13530 pierre 216
                QTreeWidgetItemIterator it(helpNav->bookmarksView);
10108 cbradney 217
                while (*it)
218
                {
23223 jghali 219
                        if (m_bookmarkIndex.contains((*it)->text(0)))
10108 cbradney 220
                        {
23223 jghali 221
                                QString pageTitle(m_bookmarkIndex.value((*it)->text(0)).first);
222
                                QString fileName(m_bookmarkIndex.value((*it)->text(0)).second);
23219 jghali 223
                                stream << "\t<item title=\"" << (*it)->text(0) << "\" pagetitle=\"" << pageTitle << "\" url=\"" << fileName << "\" />\n";
10108 cbradney 224
                        }
225
                        ++it;
226
                }
227
                stream << "</bookmarks>\n";
228
                bookFile.close();
4519 subik 229
        }
10108 cbradney 230
        // history
231
        QFile histFile(historyFile());
232
        if (histFile.open(QIODevice::WriteOnly))
233
        {
234
                QTextStream stream(&histFile);
235
                stream.setCodec("UTF-8");
236
                stream << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
237
                stream << "<history>\n";
24681 jghali 238
                for (auto it = m_history.cbegin() ; it != m_history.cend(); ++it)
10427 cbradney 239
                        stream << "\t<item title=\"" << it.value().title << "\" url=\"" << it.value().url << "\" />\n";
10108 cbradney 240
                stream << "</history>\n";
241
                histFile.close();
242
        }
243
        // size
23223 jghali 244
        m_prefs->set("xsize", width());
245
        m_prefs->set("ysize", height());
10108 cbradney 246
 
247
        emit closed();
4519 subik 248
}
2052 subik 249
 
10108 cbradney 250
void HelpBrowser::setupLocalUI()
4676 cbradney 251
{
24681 jghali 252
        IconManager& iconManager = IconManager::instance();
253
 
254
        setWindowIcon(iconManager.loadIcon("AppIcon.png"));
10108 cbradney 255
        //Add Menus
23206 jghali 256
        fileMenu = menuBar()->addMenu("");
257
        editMenu = menuBar()->addMenu("");
258
        bookMenu = menuBar()->addMenu("");
259
        histMenu = new QMenu(this);
10108 cbradney 260
 
261
        //Add Menu items
24681 jghali 262
        filePrint = fileMenu->addAction(iconManager.loadIcon("16/document-print.png"), "", this, SLOT(print()), Qt::CTRL+Qt::Key_P);
10511 fschmid 263
        fileMenu->addSeparator();
24681 jghali 264
        fileExit = fileMenu->addAction(iconManager.loadIcon("exit.png"), "", this, SLOT(close()), Qt::CTRL+Qt::Key_W);
265
        editFind = editMenu->addAction(iconManager.loadIcon("find.png"), "", this, SLOT(find()), Qt::CTRL+Qt::Key_F);
23206 jghali 266
        editFindNext = editMenu->addAction( "", this, SLOT(findNext()), Qt::Key_F3);
267
        editFindPrev = editMenu->addAction( "", this, SLOT(findPrevious()), Qt::SHIFT+Qt::Key_F3);
268
        bookAdd = bookMenu->addAction( "", this, SLOT(bookmarkButton_clicked()), Qt::CTRL+Qt::Key_D);
269
        bookDel = bookMenu->addAction( "", this, SLOT(deleteBookmarkButton_clicked()));
270
        bookDelAll = bookMenu->addAction( "", this, SLOT(deleteAllBookmarkButton_clicked()));
10108 cbradney 271
 
272
        //Add Toolbar items
24681 jghali 273
        goHome = toolBar->addAction(iconManager.loadIcon("16/go-home.png"), "", textBrowser, SLOT(home()));
274
        goBack = toolBar->addAction(iconManager.loadIcon("16/go-previous.png"), "", textBrowser, SLOT(backward()));
275
        goFwd = toolBar->addAction(iconManager.loadIcon("16/go-next.png"), "", textBrowser, SLOT(forward()));
10108 cbradney 276
        goBack->setMenu(histMenu);
277
 
13530 pierre 278
        helpNav->listView->header()->hide();
279
        helpNav->searchingView->header()->hide();
280
        helpNav->bookmarksView->header()->hide();
10108 cbradney 281
 
13530 pierre 282
//      splitter->setStretchFactor(splitter->indexOf(tabWidget), 0);
283
//      splitter->setStretchFactor(splitter->indexOf(textBrowser), 1);
10108 cbradney 284
        // reset previous size
23223 jghali 285
        m_prefs = PrefsManager::instance().prefsFile->getPluginContext("helpbrowser");
286
        int xsize = m_prefs->getUInt("xsize", 640);
287
        int ysize = m_prefs->getUInt("ysize", 480);
10108 cbradney 288
        resize(QSize(xsize, ysize).expandedTo(minimumSizeHint()) );
289
 
290
        //basic ui
291
        connect(histMenu, SIGNAL(triggered(QAction*)), this, SLOT(histChosen(QAction*)));
292
        // searching
13530 pierre 293
        connect(helpNav->searchingEdit, SIGNAL(returnPressed()), this, SLOT(searchingButton_clicked()));
294
        connect(helpNav->searchingButton, SIGNAL(clicked()), this, SLOT(searchingButton_clicked()));
295
        connect(helpNav->searchingView, SIGNAL(itemClicked( QTreeWidgetItem *, int)), this, SLOT(itemSearchSelected(QTreeWidgetItem *, int)));
10108 cbradney 296
        // bookmarks
13530 pierre 297
        connect(helpNav->bookmarkButton, SIGNAL(clicked()), this, SLOT(bookmarkButton_clicked()));
298
        connect(helpNav->deleteBookmarkButton, SIGNAL(clicked()), this, SLOT(deleteBookmarkButton_clicked()));
299
        connect(helpNav->deleteAllBookmarkButton, SIGNAL(clicked()), this, SLOT(deleteAllBookmarkButton_clicked()));
300
        connect(helpNav->bookmarksView, SIGNAL(itemClicked( QTreeWidgetItem *, int)), this, SLOT(itemBookmarkSelected(QTreeWidgetItem *, int)));
10120 fschmid 301
        // links hoover
13530 pierre 302
//      connect(textBrowser, SIGNAL(overLink(const QString &)), this, SLOT(showLinkContents(const QString &)));
303
 
304
        // status bar
20227 craig 305
//      connect(textBrowser,SIGNAL(statusBarMessage(QString)), this->statusBar(), SLOT(showMessage(QString)));
10295 cbradney 306
 
307
        languageChange();
4676 cbradney 308
}
309
 
13530 pierre 310
//void HelpBrowser::showLinkContents(const QString &link)
311
//{
312
//      statusBar()->showMessage(link);
313
//}
314
 
23223 jghali 315
void HelpBrowser::addHistoryItem(const histd2& histItem)
316
{
317
        m_history[histMenu->addAction(histItem.title)] = histItem;
318
}
319
 
10859 cbradney 320
void HelpBrowser::changeEvent(QEvent *e)
321
{
322
        if (e->type() == QEvent::LanguageChange)
323
        {
324
                languageChange();
325
        }
10903 cbradney 326
        else
327
                QWidget::changeEvent(e);
10859 cbradney 328
}
329
 
20227 craig 330
void HelpBrowser::setHtml(const QString& str)
331
{
332
        textBrowser->setHtml(str);
333
}
334
 
10108 cbradney 335
void HelpBrowser::languageChange()
4676 cbradney 336
{
10495 cbradney 337
        setWindowTitle( tr( "Scribus Online Help" ) );
10295 cbradney 338
 
339
        fileMenu->setTitle(tr("&File"));
340
        editMenu->setTitle(tr("&Edit"));
341
        bookMenu->setTitle(tr("&Bookmarks"));
342
 
343
        filePrint->setText(tr("&Print..."));
344
        fileExit->setText(tr("&Quit"));
345
        editFind->setText(tr("&Find..."));
346
        editFindNext->setText(tr("Find &Next"));
347
        editFindPrev->setText(tr("Find &Previous"));
348
        bookAdd->setText(tr("&Add Bookmark"));
349
        bookDel->setText(tr("&Delete"));
350
        bookDelAll->setText(tr("D&elete All"));
351
        Ui::HelpBrowser::retranslateUi(this);
23206 jghali 352
 
353
        if (firstRun)
10301 cbradney 354
        {
23206 jghali 355
                firstRun = false;
356
                return;
10301 cbradney 357
        }
23206 jghali 358
 
359
        QString fname(QDir::cleanPath(textBrowser->source().toLocalFile()));
360
        QFileInfo fi(fname);
23219 jghali 361
        QString fileName(fi.fileName());
23206 jghali 362
        if (ScCore->getGuiLanguage().isEmpty())
23223 jghali 363
                m_language = "en";
10301 cbradney 364
        else
23223 jghali 365
                m_language = ScCore->getGuiLanguage();
23206 jghali 366
        loadMenu();
23223 jghali 367
        if (m_menuModel != nullptr)
23219 jghali 368
                loadHelp(fileName);
10108 cbradney 369
}
370
 
371
void HelpBrowser::print()
6584 fschmid 372
{
10108 cbradney 373
        QPrinter printer;
374
        printer.setFullPage(true);
10511 fschmid 375
        QPrintDialog dialog(&printer, this);
376
        if (dialog.exec())
377
                textBrowser->print(&printer);
6584 fschmid 378
}
379
 
10108 cbradney 380
void HelpBrowser::searchingButton_clicked()
3 paul 381
{
10108 cbradney 382
        // root files
14682 fschmid 383
        helpNav->searchingView->clear();
12658 fschmid 384
        QApplication::changeOverrideCursor(QCursor(Qt::WaitCursor));
23223 jghali 385
        searchingInDirectory(m_finalBaseDir);
12658 fschmid 386
        QApplication::changeOverrideCursor(Qt::ArrowCursor);
10108 cbradney 387
}
3 paul 388
 
10108 cbradney 389
void HelpBrowser::searchingInDirectory(const QString& aDir)
390
{
16577 craig 391
        QDir dir(QDir::toNativeSeparators(aDir + "/"));
10511 fschmid 392
        QStringList in;
393
        in.append("*.html");
24681 jghali 394
        QStringList dirEntries = dir.entryList(in);
395
        for (auto it = dirEntries.cbegin(); it != dirEntries.cend(); ++it)
10108 cbradney 396
        {
10676 cbradney 397
                QString fname(aDir + "/" + (*it));
10108 cbradney 398
                QFile f(fname);
399
                if (f.open(QIODevice::ReadOnly))
400
                {
401
                        QTextStream stream(&f);
402
                        QString str = stream.readAll();
13530 pierre 403
                        int cnt = str.count(helpNav->searchingEdit->text(), Qt::CaseInsensitive);
10108 cbradney 404
                        if (cnt > 0)
405
                        {
23223 jghali 406
                                QString toFind(fname.remove(m_finalBaseDir + "/"));
407
                                QMapIterator<QString, QString> i(m_quickHelpIndex);
10676 cbradney 408
                                while (i.hasNext())
10108 cbradney 409
                                {
410
                                        i.next();
23206 jghali 411
                                        if (i.value() == toFind)
13530 pierre 412
                                                helpNav->searchingView->addTopLevelItem(new QTreeWidgetItem(helpNav->searchingView, QStringList() << i.key()));
10108 cbradney 413
                                }
414
                        }
415
                        f.close();
416
                }
417
        }
418
        // get dirs - ugly recursion
10511 fschmid 419
        in.clear();
420
        in.append("*");
421
        QStringList dst = dir.entryList(in, QDir::Dirs);
24681 jghali 422
        for (auto it = dst.cbegin(); it != dst.cend(); ++it)
423
        {
424
                if ((*it) == "." || (*it) == "..")
425
                        continue;
426
                searchingInDirectory(QDir::toNativeSeparators(aDir + QString((*it)) + "/"));
427
        }
10108 cbradney 428
}
3 paul 429
 
10108 cbradney 430
void HelpBrowser::find()
431
{
23223 jghali 432
        m_findText = QInputDialog::getText( this, tr("Find"), tr("Search Term:"), QLineEdit::Normal, m_findText, nullptr);
433
        if (m_findText.isNull())
10108 cbradney 434
                return;
435
        findNext();
436
}
3 paul 437
 
10108 cbradney 438
void HelpBrowser::findNext()
439
{
23223 jghali 440
        if (m_findText.isNull())
10108 cbradney 441
        {
442
                find();
443
                return;
444
        }
445
        // find it. finally
23223 jghali 446
        textBrowser->find(m_findText, 0);
560 cbradney 447
}
448
 
10108 cbradney 449
void HelpBrowser::findPrevious()
560 cbradney 450
{
23223 jghali 451
        if (m_findText.isNull())
2048 subik 452
        {
10108 cbradney 453
                find();
454
                return;
2048 subik 455
        }
10108 cbradney 456
        // find it. finally
23223 jghali 457
        textBrowser->find(m_findText);
10108 cbradney 458
}
459
 
460
void HelpBrowser::bookmarkButton_clicked()
461
{
20227 craig 462
/*      QString title = textBrowser->title();
463
        QString fname(QDir::cleanPath(textBrowser->source().toLocalFile()));
464
        title = QInputDialog::getText(this, tr("New Bookmark"), tr("New Bookmark's Title:"), QLineEdit::Normal, title, 0);
10108 cbradney 465
        // user cancel
20227 craig 466
        if (title.isNull())
467
                return;
10676 cbradney 468
        //TODO: start storing full paths
23223 jghali 469
        QString toFind(fname.remove(QDir::toNativeSeparators(m_finalBaseDir)));
10676 cbradney 470
        toFind=toFind.mid(1, toFind.length()-1);
23223 jghali 471
        QMapIterator<QString, QString> i(m_quickHelpIndex);
10301 cbradney 472
        while (i.hasNext())
2052 subik 473
        {
10108 cbradney 474
                i.next();
23206 jghali 475
                if (i.value() == toFind)
10108 cbradney 476
                {
23223 jghali 477
                        m_bookmarkIndex.insert(title, qMakePair(i.key(), i.value()));
13530 pierre 478
                        helpNav->bookmarksView->addTopLevelItem(new QTreeWidgetItem(helpNav->bookmarksView, QStringList() << title));
10108 cbradney 479
                }
2052 subik 480
        }
20227 craig 481
*/
560 cbradney 482
}
483
 
10108 cbradney 484
void HelpBrowser::deleteBookmarkButton_clicked()
560 cbradney 485
{
23219 jghali 486
        QTreeWidgetItem *twi = helpNav->bookmarksView->currentItem();
23206 jghali 487
        if (twi != nullptr)
10676 cbradney 488
        {
23223 jghali 489
                if (m_bookmarkIndex.contains(twi->text(0)))
490
                        m_bookmarkIndex.remove(twi->text(0));
10676 cbradney 491
                delete twi;
492
        }
10108 cbradney 493
}
560 cbradney 494
 
10108 cbradney 495
void HelpBrowser::deleteAllBookmarkButton_clicked()
496
{
23223 jghali 497
        m_bookmarkIndex.clear();
13530 pierre 498
        helpNav->bookmarksView->clear();
560 cbradney 499
}
500
 
10108 cbradney 501
void HelpBrowser::histChosen(QAction* i)
560 cbradney 502
{
23223 jghali 503
        if (m_history.contains(i))
504
                textBrowser->setSource( QUrl::fromLocalFile(m_history[i].url) );
560 cbradney 505
}
506
 
16180 craig 507
void HelpBrowser::jumpToHelpSection(const QString& jumpToSection, const QString& jumpToFile, bool dontChangeIfAlreadyLoaded)
560 cbradney 508
{
509
        QString toLoad;
2877 cbradney 510
        if (jumpToFile.isEmpty())
564 cbradney 511
        {
2877 cbradney 512
                if (jumpToSection.isEmpty())
564 cbradney 513
                {
23223 jghali 514
                        QModelIndex index = m_menuModel->index(0, 1);
10108 cbradney 515
                        if (index.isValid())
1141 cbradney 516
                        {
13530 pierre 517
                                helpNav->listView->selectionModel()->select(index, QItemSelectionModel::ClearAndSelect);
23223 jghali 518
                                toLoad = m_menuModel->data(index, Qt::DisplayRole).toString();
1141 cbradney 519
                        }
205 Franz 520
                }
23219 jghali 521
                else if (jumpToSection == "scripter")
564 cbradney 522
                {
23206 jghali 523
                        toLoad += "scripter1.html";
560 cbradney 524
                }
205 Franz 525
        }
560 cbradney 526
        else
16180 craig 527
        {
23219 jghali 528
                toLoad = jumpToFile;
16180 craig 529
        }
23219 jghali 530
        if (!toLoad.isEmpty())
1141 cbradney 531
                loadHelp(toLoad);
23206 jghali 532
        else if (!dontChangeIfAlreadyLoaded)
533
                displayNoHelp();
3 paul 534
}
10676 cbradney 535
 
23219 jghali 536
void HelpBrowser::loadHelp(const QString& fileName)
68 Franz 537
{
10108 cbradney 538
        struct histd2 his;
23219 jghali 539
        bool isAvail = false;
560 cbradney 540
        QString toLoad;
23219 jghali 541
 
542
        QFileInfo fi(fileName);
23206 jghali 543
        if (fi.fileName().length() > 0)
564 cbradney 544
        {
23219 jghali 545
                if (fi.isAbsolute() && fi.exists())
546
                        toLoad = fileName;
547
                if (toLoad.isEmpty())
564 cbradney 548
                {
23219 jghali 549
                        QStringList searchPaths = textBrowser->searchPaths();
550
                        for (int i = 0; i < searchPaths.count(); ++i)
551
                        {
552
                                const QString& searchPath = searchPaths.at(i);
553
                                fi = QFileInfo(searchPath + "/" + fi.fileName());
554
                                if (fi.exists())
555
                                {
23222 jghali 556
                                        toLoad = fi.fileName();
23219 jghali 557
                                        break;
558
                                }
559
                        }
564 cbradney 560
                }
23219 jghali 561
                if (toLoad.isEmpty())
562
                {
563
                        QStringList searchPaths = textBrowser->searchPaths();
564
                        for (int i = 0; i < searchPaths.count(); ++i)
565
                        {
566
                                const QString& searchPath = searchPaths.at(i);
567
                                fi = QFileInfo(searchPath + "/index.html");
568
                                if (fi.exists())
569
                                {
23222 jghali 570
                                        toLoad = "index.html";
23219 jghali 571
                                        break;
572
                                }
573
                        }
574
                }
575
                isAvail = fi.exists();
564 cbradney 576
        }
23219 jghali 577
        if (isAvail)
564 cbradney 578
        {
20233 jghali 579
                textBrowser->setSource(QUrl::fromLocalFile(toLoad));
10660 cbradney 580
 
20227 craig 581
//              his.title = textBrowser->title();
2877 cbradney 582
                if (his.title.isEmpty())
2052 subik 583
                        his.title = toLoad;
584
                his.url = toLoad;
23223 jghali 585
                m_history[histMenu->addAction(his.title)] = his;
564 cbradney 586
        }
20233 jghali 587
        else
588
        {
589
                displayNoHelp();
590
        }
23223 jghali 591
        if (m_history.count() > 15)
2052 subik 592
        {
23206 jghali 593
                QAction* first = histMenu->actions().first();
23223 jghali 594
                m_history.remove(first);
10108 cbradney 595
                histMenu->removeAction(first);
2052 subik 596
        }
560 cbradney 597
}
598
 
1092 cbradney 599
void HelpBrowser::loadMenu()
560 cbradney 600
{
21526 craig 601
        QString baseHelpDir = ScPaths::instance().userHelpFilesDir(false);
21029 craig 602
        QString installHelpDir  = ScPaths::instance().docDir();
603
 
23219 jghali 604
        QVector<QPair<QString, QString> > helpMenuFiles;
605
        helpMenuFiles.reserve(6);
23223 jghali 606
        helpMenuFiles.append(qMakePair(baseHelpDir, m_language));
607
        helpMenuFiles.append(qMakePair(installHelpDir, m_language));
608
        if (!m_language.isEmpty())
793 cbradney 609
        {
24202 craig 610
                QStringList langDecomp = m_language.split(QChar('_'), Qt::SkipEmptyParts);
23219 jghali 611
                QString altLanguage = langDecomp.first();
612
                helpMenuFiles.append(qMakePair(baseHelpDir, altLanguage));
613
                helpMenuFiles.append(qMakePair(installHelpDir, altLanguage));
614
        }
615
        QString enLanguage("en");
616
        helpMenuFiles.append(qMakePair(baseHelpDir, enLanguage));
617
        helpMenuFiles.append(qMakePair(installHelpDir, enLanguage));
618
 
24467 jghali 619
        QString toLoad;
23219 jghali 620
        for (int i = 0; i < helpMenuFiles.count(); ++i)
621
        {
622
                auto helpPair = helpMenuFiles.at(i);
623
                QString helpFile = QDir::toNativeSeparators(helpPair.first + helpPair.second + "/menu.xml");
624
                QFileInfo helpInfo(helpFile);
625
                if (helpInfo.exists())
10312 cbradney 626
                {
23219 jghali 627
                        toLoad = helpFile;
23223 jghali 628
                        m_language = helpPair.second;
23219 jghali 629
                        break;
10676 cbradney 630
                }
786 cbradney 631
        }
23219 jghali 632
 
633
        if (toLoad.isEmpty())
21029 craig 634
        {
23223 jghali 635
                m_language = "en";
636
                toLoad = QDir::toNativeSeparators(installHelpDir + m_language + "/menu.xml");
21029 craig 637
        }
23219 jghali 638
 
10676 cbradney 639
        //Set our final location for loading the help files
23219 jghali 640
        QFileInfo baseFi = QFileInfo(toLoad);
23223 jghali 641
        m_finalBaseDir = baseFi.path();
23219 jghali 642
        QStringList searchPaths;
24681 jghali 643
        searchPaths << m_finalBaseDir;
644
        searchPaths << m_defaultBaseDir;
23219 jghali 645
        textBrowser->setSearchPaths(searchPaths);
646
 
23223 jghali 647
        delete m_menuModel;
648
        m_menuModel = nullptr;
23219 jghali 649
 
10676 cbradney 650
        if (baseFi.exists())
564 cbradney 651
        {
23223 jghali 652
                m_menuModel = new ScHelpTreeModel(toLoad, "Topic", "Location", &m_quickHelpIndex);
10676 cbradney 653
 
23223 jghali 654
                helpNav->listView->setModel(m_menuModel);
13530 pierre 655
                helpNav->listView->setSelectionMode(QAbstractItemView::SingleSelection);
23223 jghali 656
                QItemSelectionModel *selectionModel = new QItemSelectionModel(m_menuModel);
13530 pierre 657
                helpNav->listView->setSelectionModel(selectionModel);
658
                connect(helpNav->listView->selectionModel(), SIGNAL(selectionChanged( const QItemSelection &, const QItemSelection &)), this, SLOT(itemSelected( const QItemSelection &, const QItemSelection &)));
10676 cbradney 659
 
13530 pierre 660
                helpNav->listView->setColumnHidden(1,true);
10108 cbradney 661
        }
68 Franz 662
}
663
 
10108 cbradney 664
void HelpBrowser::readBookmarks()
68 Franz 665
{
24229 jghali 666
        BookmarkParser2 bookmarkParser;
667
        bookmarkParser.view = helpNav->bookmarksView;
668
        bookmarkParser.quickHelpIndex = &m_quickHelpIndex;
669
        bookmarkParser.bookmarkIndex = &m_bookmarkIndex;
670
        bookmarkParser.parse(bookmarkFile());
68 Franz 671
}
2006 subik 672
 
10108 cbradney 673
void HelpBrowser::readHistory()
2006 subik 674
{
24229 jghali 675
        HistoryParser2 historyParser;
676
        historyParser.helpBrowser = this;
677
        historyParser.parse(historyFile());
2006 subik 678
}
679
 
10108 cbradney 680
void HelpBrowser::itemSelected(const QItemSelection & selected, const QItemSelection & deselected)
681
{
682
        Q_UNUSED(deselected);
4905 subik 683
 
22665 jghali 684
        int i = 0;
685
        const QModelIndexList items = selected.indexes();
686
        for (const QModelIndex& index : items)
2006 subik 687
        {
22665 jghali 688
                if (i == 1) // skip 0, as this is always the rootitem, even if we are selecting the rootitem. hmm
2006 subik 689
                {
23223 jghali 690
                        QString fileName(m_menuModel->data(index, Qt::DisplayRole).toString());
23219 jghali 691
                        if (!fileName.isEmpty())
2006 subik 692
                        {
23219 jghali 693
                                loadHelp(fileName);
2006 subik 694
                        }
695
                }
10108 cbradney 696
                ++i;
2006 subik 697
        }
698
}
699
 
10108 cbradney 700
void HelpBrowser::itemSearchSelected(QTreeWidgetItem *twi, int i)
2006 subik 701
{
10108 cbradney 702
        Q_UNUSED(i);
703
        if (!twi)
704
                return;
23223 jghali 705
        if (m_quickHelpIndex.contains(twi->text(0)))
10108 cbradney 706
        {
23223 jghali 707
                QString fileName(m_quickHelpIndex.value(twi->text(0)));
23219 jghali 708
                if (!fileName.isEmpty())
10108 cbradney 709
                {
23219 jghali 710
                        loadHelp(fileName);
23223 jghali 711
                        m_findText = helpNav->searchingEdit->text();
10108 cbradney 712
                        findNext();
713
                }
714
        }
2006 subik 715
}
716
 
10108 cbradney 717
void HelpBrowser::itemBookmarkSelected(QTreeWidgetItem *twi, int i)
2006 subik 718
{
10108 cbradney 719
        Q_UNUSED(i);
720
        if (!twi)
2006 subik 721
                return;
23223 jghali 722
        if (m_bookmarkIndex.contains(twi->text(0)))
2006 subik 723
        {
23223 jghali 724
                QString fileName(m_bookmarkIndex.value(twi->text(0)).second);
23219 jghali 725
                if (!fileName.isEmpty())
726
                        loadHelp(fileName);
2006 subik 727
        }
728
}
2023 subik 729
 
10108 cbradney 730
/*! \brief Returns the name of the cfg file for bookmarks.
731
A helper function.
732
\author Petr Vanek <petr@yarpen.cz>
733
*/
734
QString HelpBrowser::bookmarkFile()
2023 subik 735
{
21526 craig 736
        QString appDataDir(ScPaths::applicationDataDir());
10108 cbradney 737
        QString fname(appDataDir + "doc/bookmarks.xml");
738
        if (!QFile::exists(fname))
2023 subik 739
        {
16577 craig 740
                QDir d(QDir::toNativeSeparators(appDataDir));
10108 cbradney 741
                d.mkdir("doc");
2023 subik 742
        }
10108 cbradney 743
        return fname;
2023 subik 744
}
745
 
746
 
10108 cbradney 747
/*! \brief Returns the name of the cfg file for persistent history.
748
A helper function.
749
\author Petr Vanek <petr@yarpen.cz>
750
*/
751
QString HelpBrowser::historyFile()
2048 subik 752
{
21526 craig 753
        QString appDataDir(ScPaths::applicationDataDir());
10108 cbradney 754
        QString fname(appDataDir + "doc/history.xml");
755
        if (!QFile::exists(fname))
756
        {
16577 craig 757
                QDir d(QDir::toNativeSeparators(appDataDir));
10108 cbradney 758
                d.mkdir("doc");
759
        }
760
        return fname;
2048 subik 761
}
10676 cbradney 762
 
763
void HelpBrowser::displayNoHelp()
764
{
765
        QString noHelpMsg=tr("<h2><p>Sorry, no manual is installed!</p><p>Please see:</p><ul><li>http://docs.scribus.net for updated documentation</li><li>http://www.scribus.net for downloads</li></ul></h2>",
766
                                                 "HTML message for no documentation available to show");
767
 
20233 jghali 768
        textBrowser->setHtml(noHelpMsg);
10676 cbradney 769
 
770
        filePrint->setEnabled(false);
771
        editFind->setEnabled(false);
772
        editFindNext->setEnabled(false);
773
        editFindPrev->setEnabled(false);
774
        bookAdd->setEnabled(false);
775
        bookDel->setEnabled(false);
776
        bookDelAll->setEnabled(false);
777
        goHome->setEnabled(false);
778
        goBack->setEnabled(false);
779
        goFwd->setEnabled(false);
780
 
781
        histMenu->disconnect();
13530 pierre 782
        helpNav->searchingEdit->disconnect();
783
        helpNav->searchingButton->disconnect();
784
        helpNav->searchingView->disconnect();
785
        helpNav->bookmarkButton->disconnect();
786
        helpNav->deleteBookmarkButton->disconnect();
787
        helpNav->deleteAllBookmarkButton->disconnect();
788
        helpNav->bookmarksView->disconnect();
10676 cbradney 789
        textBrowser->disconnect();
790
}