1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2009 Tony Murray <murraytony @ gmail.com> |
4 | ** |
5 | ** This file is part of KDE. |
6 | ** |
7 | ** This program is free software; you can redistribute it and/or modify |
8 | ** it under the terms of the GNU General Public License as published by |
9 | ** the Free Software Foundation; either version 2 of the License, or |
10 | ** (at your option) any later version. |
11 | ** |
12 | ** This program is distributed in the hope that it will be useful, |
13 | ** but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 | ** GNU General Public License for more details. |
16 | ** |
17 | ** You should have received a copy of the GNU General Public License |
18 | ** along with this program; see the file COPYING. If not, write to |
19 | ** the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
20 | ** Boston, MA 02110-1301, USA. |
21 | ** |
22 | ****************************************************************************/ |
23 | |
24 | #include "tabbedviewwidget.h" |
25 | |
26 | TabbedViewWidgetModel::TabbedViewWidgetModel(KTabWidget *modelTarget) |
27 | : QAbstractItemModel(modelTarget), m_tabWidget(modelTarget) |
28 | { |
29 | } |
30 | |
31 | QModelIndex TabbedViewWidgetModel::index(int row, int column, const QModelIndex &parent) const |
32 | { |
33 | if (!hasIndex(row, column, parent)) { |
34 | return QModelIndex(); |
35 | } |
36 | return createIndex(row, column, m_tabWidget->widget(row)); |
37 | } |
38 | |
39 | QModelIndex TabbedViewWidgetModel::parent(const QModelIndex &) const |
40 | { |
41 | return QModelIndex(); |
42 | } |
43 | |
44 | int TabbedViewWidgetModel::columnCount(const QModelIndex &) const |
45 | { |
46 | return 1; |
47 | } |
48 | |
49 | |
50 | int TabbedViewWidgetModel::rowCount(const QModelIndex &) const |
51 | { |
52 | return m_tabWidget->count(); |
53 | } |
54 | |
55 | Qt::ItemFlags TabbedViewWidgetModel::flags(const QModelIndex &index) const |
56 | { |
57 | if (!index.isValid()) { |
58 | return Qt::ItemIsEnabled; |
59 | } |
60 | return QAbstractItemModel::flags(index) | Qt::ItemIsEditable; |
61 | } |
62 | |
63 | bool TabbedViewWidgetModel::setData(const QModelIndex &index, const QVariant &value, int role) |
64 | { |
65 | if (index.isValid() && role == Qt::EditRole) { |
66 | m_tabWidget->setTabText(index.row(), value.toString()); |
67 | emit dataChanged(index, index); |
68 | return true; |
69 | } |
70 | return false; |
71 | } |
72 | |
73 | QVariant TabbedViewWidgetModel::data(const QModelIndex &index, int role) const |
74 | { |
75 | if (!index.isValid()) { |
76 | return QVariant(); |
77 | } |
78 | |
79 | switch (role) { |
80 | case Qt::EditRole: |
81 | case Qt::DisplayRole: |
82 | return m_tabWidget->tabText(index.row()).remove(QRegExp("&(?!&)" )); //remove accelerator string |
83 | case Qt::ToolTipRole: |
84 | return m_tabWidget->tabToolTip(index.row()); |
85 | case Qt::DecorationRole: |
86 | return m_tabWidget->tabIcon(index.row()); |
87 | default: |
88 | return QVariant(); |
89 | } |
90 | } |
91 | |
92 | void TabbedViewWidgetModel::emitLayoutAboutToBeChanged() |
93 | { |
94 | emit layoutAboutToBeChanged(); |
95 | } |
96 | |
97 | void TabbedViewWidgetModel::emitLayoutChanged() |
98 | { |
99 | emit layoutChanged(); |
100 | } |
101 | |
102 | void TabbedViewWidgetModel::emitDataChanged(int index) |
103 | { |
104 | QModelIndex modelIndex = createIndex(index, 1); |
105 | emit dataChanged(modelIndex, modelIndex); |
106 | } |
107 | |
108 | TabbedViewWidget::TabbedViewWidget(QWidget *parent, Qt::WFlags flags) |
109 | : KTabWidget(parent, flags), m_model(new TabbedViewWidgetModel(this)) |
110 | { |
111 | } |
112 | |
113 | TabbedViewWidget::~TabbedViewWidget() |
114 | { |
115 | } |
116 | |
117 | TabbedViewWidgetModel* TabbedViewWidget::getModel() |
118 | { |
119 | return m_model; |
120 | } |
121 | |
122 | int TabbedViewWidget::addTab(QWidget *page, const QString &label) |
123 | { |
124 | int count = KTabWidget::count(); |
125 | m_model->beginInsertRows(QModelIndex(), count, count); |
126 | int ret = KTabWidget::addTab(page, label); |
127 | m_model->endInsertRows(); |
128 | return ret; |
129 | } |
130 | |
131 | int TabbedViewWidget::addTab(QWidget *page, const QIcon &icon, const QString &label) |
132 | { |
133 | int count = KTabWidget::count(); |
134 | m_model->beginInsertRows(QModelIndex(), count, count); |
135 | int ret = KTabWidget::addTab(page, icon, label); |
136 | m_model->endInsertRows(); |
137 | return ret; |
138 | } |
139 | |
140 | int TabbedViewWidget::insertTab(int index, QWidget *page, const QString &label) |
141 | { |
142 | m_model->beginInsertRows(QModelIndex(), index, index); |
143 | int ret = KTabWidget::insertTab(index, page, label); |
144 | m_model->endInsertRows(); |
145 | return ret; |
146 | } |
147 | |
148 | int TabbedViewWidget::insertTab(int index, QWidget *page, const QIcon &icon, const QString &label) |
149 | { |
150 | m_model->beginInsertRows(QModelIndex(), index, index); |
151 | int ret = KTabWidget::insertTab(index, page, icon, label); |
152 | m_model->endInsertRows(); |
153 | return ret; |
154 | } |
155 | |
156 | void TabbedViewWidget::removePage(QWidget *page) |
157 | { |
158 | int index = KTabWidget::indexOf(page); |
159 | m_model->beginRemoveRows(QModelIndex(), index, index); |
160 | KTabWidget::removePage(page); |
161 | m_model->endRemoveRows(); |
162 | } |
163 | |
164 | void TabbedViewWidget::removeTab(int index) |
165 | { |
166 | m_model->beginRemoveRows(QModelIndex(), index, index); |
167 | KTabWidget::removeTab(index); |
168 | m_model->endRemoveRows(); |
169 | } |
170 | |
171 | void TabbedViewWidget::moveTab(int from, int to) |
172 | { |
173 | m_model->emitLayoutAboutToBeChanged(); |
174 | KTabWidget::moveTab(from, to); |
175 | m_model->emitLayoutChanged(); |
176 | } |
177 | |
178 | void TabbedViewWidget::setTabText(int index, const QString &label) |
179 | { |
180 | KTabWidget::setTabText(index, label); |
181 | m_model->emitDataChanged(index); |
182 | } |
183 | |
184 | #include "tabbedviewwidget.moc" |
185 | |