1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the Assistant module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21 ** included in the packaging of this file. Please review the following
22 ** information to ensure the GNU General Public License requirements will
23 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24 **
25 ** $QT_END_LICENSE$
26 **
27 ****************************************************************************/
28 #include "openpagesmodel.h"
29
30 #include "helpenginewrapper.h"
31 #include "helpviewer.h"
32 #include "tracer.h"
33
34 #include <QtCore/QStringList>
35 #include <QtCore/QUrl>
36
37 QT_BEGIN_NAMESPACE
38
OpenPagesModel(QObject * parent)39 OpenPagesModel::OpenPagesModel(QObject *parent) : QAbstractTableModel(parent)
40 {
41 TRACE_OBJ
42 }
43
rowCount(const QModelIndex & parent) const44 int OpenPagesModel::rowCount(const QModelIndex &parent) const
45 {
46 TRACE_OBJ
47 return parent.isValid() ? 0 : m_pages.count();
48 }
49
columnCount(const QModelIndex &) const50 int OpenPagesModel::columnCount(const QModelIndex &/*parent*/) const
51 {
52 TRACE_OBJ
53 return 2;
54 }
55
data(const QModelIndex & index,int role) const56 QVariant OpenPagesModel::data(const QModelIndex &index, int role) const
57 {
58 TRACE_OBJ
59 if (!index.isValid() || index.row() >= rowCount() || index.column() > 0
60 || role != Qt::DisplayRole)
61 return QVariant();
62 QString title = m_pages.at(index.row())->title();
63 title.replace(QLatin1Char('&'), QLatin1String("&&"));
64 return title.isEmpty() ? QLatin1String("(Untitled)") : title;
65 }
66
addPage(const QUrl & url,qreal zoom)67 void OpenPagesModel::addPage(const QUrl &url, qreal zoom)
68 {
69 TRACE_OBJ
70 beginInsertRows(QModelIndex(), rowCount(), rowCount());
71 HelpViewer *page = new HelpViewer(zoom);
72 connect(page, &HelpViewer::titleChanged,
73 this, &OpenPagesModel::handleTitleChanged);
74 m_pages << page;
75 endInsertRows();
76 page->setSource(url);
77 }
78
removePage(int index)79 void OpenPagesModel::removePage(int index)
80 {
81 TRACE_OBJ
82 Q_ASSERT(index >= 0 && index < rowCount());
83 beginRemoveRows(QModelIndex(), index, index);
84 HelpViewer *page = m_pages.at(index);
85 m_pages.removeAt(index);
86 endRemoveRows();
87 page->deleteLater();
88 }
89
pageAt(int index) const90 HelpViewer *OpenPagesModel::pageAt(int index) const
91 {
92 TRACE_OBJ
93 Q_ASSERT(index >= 0 && index < rowCount());
94 return m_pages.at(index);
95 }
96
handleTitleChanged()97 void OpenPagesModel::handleTitleChanged()
98 {
99 TRACE_OBJ
100 HelpViewer *page = static_cast<HelpViewer *>(sender());
101 const int row = m_pages.indexOf(page);
102 Q_ASSERT(row != -1 );
103 const QModelIndex &item = index(row, 0);
104 emit dataChanged(item, item);
105 }
106
107 QT_END_NAMESPACE
108