OSDN Git Service

Update license.
[qt-creator-jp/qt-creator-jp.git] / src / plugins / help / openpagesmodel.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
8 **
9 **
10 ** GNU Lesser General Public License Usage
11 **
12 ** This file may be used under the terms of the GNU Lesser General Public
13 ** License version 2.1 as published by the Free Software Foundation and
14 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
15 ** Please review the following information to ensure the GNU Lesser General
16 ** Public License version 2.1 requirements will be met:
17 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 **
19 ** In addition, as a special exception, Nokia gives you certain additional
20 ** rights. These rights are described in the Nokia Qt LGPL Exception
21 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 **
23 ** Other Usage
24 **
25 ** Alternatively, this file may be used in accordance with the terms and
26 ** conditions contained in a signed written agreement between you and Nokia.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **************************************************************************/
32
33 #include "openpagesmodel.h"
34 #include "helpviewer.h"
35
36 #include <QtCore/QUrl>
37
38 using namespace Help::Internal;
39
40 OpenPagesModel::OpenPagesModel(QObject *parent)
41     : QAbstractTableModel(parent)
42 {
43 }
44
45 int OpenPagesModel::rowCount(const QModelIndex &parent) const
46 {
47     return  parent.isValid() ? 0 : m_pages.count();
48 }
49
50 int OpenPagesModel::columnCount(const QModelIndex &/*parent*/) const
51 {
52     return 2;
53 }
54
55 QVariant OpenPagesModel::data(const QModelIndex &index, int role) const
56 {
57     if (!index.isValid() || index.row() >= rowCount()
58         || index.column() >= columnCount() - 1)
59         return QVariant();
60
61     switch (role) {
62         case Qt::ToolTipRole:
63             return m_pages.at(index.row())->source().toString();
64         case Qt::DisplayRole: {
65             QString title = m_pages.at(index.row())->title();
66             title.replace(QLatin1Char('&'), QLatin1String("&&"));
67             return title.isEmpty() ? tr("(Untitled)") : title;
68         }
69         default:
70             break;
71     }
72     return QVariant();
73 }
74
75 void OpenPagesModel::addPage(const QUrl &url, qreal zoom)
76 {
77     beginInsertRows(QModelIndex(), rowCount(), rowCount());
78     HelpViewer *page = new HelpViewer(zoom);
79     connect(page, SIGNAL(titleChanged()), this, SLOT(handleTitleChanged()));
80     m_pages << page;
81     endInsertRows();
82     page->setSource(url);
83 }
84
85 void OpenPagesModel::removePage(int index)
86 {
87     Q_ASSERT(index >= 0 && index < rowCount());
88     beginRemoveRows(QModelIndex(), index, index);
89     HelpViewer *page = m_pages.at(index);
90     m_pages.removeAt(index);
91     endRemoveRows();
92     page->deleteLater();
93 }
94
95 HelpViewer *OpenPagesModel::pageAt(int index) const
96 {
97     Q_ASSERT(index >= 0 && index < rowCount());
98     return m_pages.at(index);
99 }
100
101 void OpenPagesModel::handleTitleChanged()
102 {
103     HelpViewer *page = static_cast<HelpViewer *>(sender());
104     const int row = m_pages.indexOf(page);
105     Q_ASSERT(row != -1 );
106     const QModelIndex &item = index(row, 0);
107     emit dataChanged(item, item);
108 }