]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphincontroller.cpp
increase the readability of the code
[dolphin.git] / src / dolphincontroller.cpp
1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz (peter.penz@gmx.at) *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 ***************************************************************************/
19
20 #include "dolphincontroller.h"
21
22 #include <kdirmodel.h>
23 #include <QAbstractProxyModel>
24 #include <QApplication>
25
26 DolphinController::DolphinController(DolphinView* dolphinView) :
27 QObject(dolphinView),
28 m_zoomInPossible(false),
29 m_zoomOutPossible(false),
30 m_openTab(false),
31 m_url(),
32 m_dolphinView(dolphinView),
33 m_itemView(0)
34 {
35 }
36
37 DolphinController::~DolphinController()
38 {
39 }
40
41 void DolphinController::setUrl(const KUrl& url)
42 {
43 if (m_url != url) {
44 m_url = url;
45 emit urlChanged(url);
46 }
47 }
48
49 void DolphinController::setItemView(QAbstractItemView* view)
50 {
51 if (m_itemView != 0) {
52 disconnect(m_itemView, SIGNAL(pressed(const QModelIndex&)),
53 this, SLOT(updateOpenTabState()));
54 }
55
56 m_itemView = view;
57
58 // TODO: this is a workaround until Qt-issue 176832 has been fixed
59 connect(m_itemView, SIGNAL(pressed(const QModelIndex&)),
60 this, SLOT(updateOpenTabState()));
61 }
62
63 void DolphinController::triggerUrlChangeRequest(const KUrl& url)
64 {
65 if (m_url != url) {
66 emit requestUrlChange(url);
67 }
68 }
69
70 void DolphinController::triggerContextMenuRequest(const QPoint& pos)
71 {
72 emit activated();
73 emit requestContextMenu(pos);
74 }
75
76 void DolphinController::requestActivation()
77 {
78 emit activated();
79 }
80
81 void DolphinController::indicateDroppedUrls(const KUrl::List& urls,
82 const KUrl& destPath,
83 const KFileItem& destItem)
84 {
85 emit urlsDropped(urls, destPath, destItem);
86 }
87
88
89 void DolphinController::indicateSortingChange(DolphinView::Sorting sorting)
90 {
91 emit sortingChanged(sorting);
92 }
93
94 void DolphinController::indicateSortOrderChange(Qt::SortOrder order)
95 {
96 emit sortOrderChanged(order);
97 }
98
99 void DolphinController::indicateAdditionalInfoChange(const KFileItemDelegate::InformationList& info)
100 {
101 emit additionalInfoChanged(info);
102 }
103
104 void DolphinController::indicateActivationChange(bool active)
105 {
106 emit activationChanged(active);
107 }
108
109 void DolphinController::triggerZoomIn()
110 {
111 emit zoomIn();
112 }
113
114 void DolphinController::triggerZoomOut()
115 {
116 emit zoomOut();
117 }
118
119 void DolphinController::handleKeyPressEvent(QKeyEvent* event)
120 {
121 Q_ASSERT(m_itemView != 0);
122
123 const QItemSelectionModel* selModel = m_itemView->selectionModel();
124 const QModelIndex currentIndex = selModel->currentIndex();
125 const bool trigger = currentIndex.isValid()
126 && (event->key() == Qt::Key_Return)
127 && (selModel->selectedIndexes().count() > 0);
128 if (trigger) {
129 const QModelIndexList indexList = selModel->selectedIndexes();
130 foreach (const QModelIndex& index, indexList) {
131 triggerItem(index);
132 }
133 }
134 }
135
136 KFileItem DolphinController::itemForIndex(const QModelIndex& index) const
137 {
138 Q_ASSERT(m_itemView != 0);
139
140 QAbstractProxyModel* proxyModel = static_cast<QAbstractProxyModel*>(m_itemView->model());
141 KDirModel* dirModel = static_cast<KDirModel*>(proxyModel->sourceModel());
142 const QModelIndex dirIndex = proxyModel->mapToSource(index);
143 return dirModel->itemForIndex(dirIndex);
144 }
145
146 void DolphinController::triggerItem(const QModelIndex& index)
147 {
148 const bool openTab = m_openTab;
149 m_openTab = false;
150
151 const KFileItem item = itemForIndex(index);
152 if (index.isValid() && (index.column() == KDirModel::Name)) {
153 if (openTab && (item.isDir() || m_dolphinView->isTabsForFilesEnabled())) {
154 emit tabRequested(item.url());
155 } else {
156 emit itemTriggered(item);
157 }
158 } else {
159 m_itemView->clearSelection();
160 if (!openTab) {
161 emit itemEntered(item);
162 }
163 }
164 }
165
166 void DolphinController::emitItemEntered(const QModelIndex& index)
167 {
168 KFileItem item = itemForIndex(index);
169 if (!item.isNull()) {
170 emit itemEntered(item);
171 }
172 }
173
174 void DolphinController::emitViewportEntered()
175 {
176 emit viewportEntered();
177 }
178
179 void DolphinController::updateOpenTabState()
180 {
181 m_openTab = QApplication::mouseButtons() & Qt::MidButton;
182 }
183
184 #include "dolphincontroller.moc"