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
37QT_BEGIN_NAMESPACE
38
39OpenPagesModel::OpenPagesModel(QObject *parent) : QAbstractTableModel(parent)
40{
41 TRACE_OBJ
42}
43
44int OpenPagesModel::rowCount(const QModelIndex &parent) const
45{
46 TRACE_OBJ
47 return parent.isValid() ? 0 : m_pages.count();
48}
49
50int OpenPagesModel::columnCount(const QModelIndex &/*parent*/) const
51{
52 TRACE_OBJ
53 return 2;
54}
55
56QVariant 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(i: index.row())->title();
63 title.replace(c: QLatin1Char('&'), after: QLatin1String("&&"));
64 return title.isEmpty() ? QLatin1String("(Untitled)") : title;
65}
66
67void OpenPagesModel::addPage(const QUrl &url, qreal zoom)
68{
69 TRACE_OBJ
70 beginInsertRows(parent: QModelIndex(), first: rowCount(), last: rowCount());
71 HelpViewer *page = new HelpViewer(zoom);
72 connect(sender: page, signal: &HelpViewer::titleChanged,
73 receiver: this, slot: &OpenPagesModel::handleTitleChanged);
74 m_pages << page;
75 endInsertRows();
76 page->setSource(url);
77}
78
79void OpenPagesModel::removePage(int index)
80{
81 TRACE_OBJ
82 Q_ASSERT(index >= 0 && index < rowCount());
83 beginRemoveRows(parent: QModelIndex(), first: index, last: index);
84 HelpViewer *page = m_pages.at(i: index);
85 m_pages.removeAt(i: index);
86 endRemoveRows();
87 page->deleteLater();
88}
89
90HelpViewer *OpenPagesModel::pageAt(int index) const
91{
92 TRACE_OBJ
93 Q_ASSERT(index >= 0 && index < rowCount());
94 return m_pages.at(i: index);
95}
96
97void OpenPagesModel::handleTitleChanged()
98{
99 TRACE_OBJ
100 HelpViewer *page = static_cast<HelpViewer *>(sender());
101 const int row = m_pages.indexOf(t: page);
102 Q_ASSERT(row != -1 );
103 const QModelIndex &item = index(row, column: 0);
104 emit dataChanged(topLeft: item, bottomRight: item);
105}
106
107QT_END_NAMESPACE
108

source code of qttools/src/assistant/assistant/openpagesmodel.cpp