]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/information/informationpanel.cpp
avoid double margins
[dolphin.git] / src / panels / information / informationpanel.cpp
1 /***************************************************************************
2 * Copyright (C) 2006-2009 by Peter Penz <peter.penz19@gmail.com> *
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 "informationpanel.h"
21
22 #include "informationpanelcontent.h"
23 #include <KIO/Job>
24 #include <KDirNotify>
25 #include <QApplication>
26 #include <QShowEvent>
27 #include <QVBoxLayout>
28
29 InformationPanel::InformationPanel(QWidget* parent) :
30 Panel(parent),
31 m_initialized(false),
32 m_infoTimer(0),
33 m_urlChangedTimer(0),
34 m_resetUrlTimer(0),
35 m_shownUrl(),
36 m_urlCandidate(),
37 m_invalidUrlCandidate(),
38 m_fileItem(),
39 m_selection(),
40 m_folderStatJob(0),
41 m_content(0)
42 {
43 }
44
45 InformationPanel::~InformationPanel()
46 {
47 }
48
49 void InformationPanel::setSelection(const KFileItemList& selection)
50 {
51 if (!isVisible()) {
52 return;
53 }
54
55 if (selection.isEmpty() && m_selection.isEmpty()) {
56 // The selection has not really changed, only the current index.
57 // QItemSelectionModel emits a signal in this case and it is less
58 // expensive doing the check this way instead of patching
59 // DolphinView::emitSelectionChanged().
60 return;
61 }
62
63 m_selection = selection;
64 m_fileItem = KFileItem();
65
66 const int count = selection.count();
67 if (count == 0) {
68 if (!isEqualToShownUrl(url())) {
69 m_shownUrl = url();
70 showItemInfo();
71 }
72 } else {
73 if ((count == 1) && !selection.first().url().isEmpty()) {
74 m_urlCandidate = selection.first().url();
75 }
76 m_infoTimer->start();
77 }
78 }
79
80 void InformationPanel::requestDelayedItemInfo(const KFileItem& item)
81 {
82 if (!isVisible() || (item.isNull() && m_fileItem.isNull())) {
83 return;
84 }
85
86 if (QApplication::mouseButtons() & Qt::LeftButton) {
87 // Ignore the request of an item information when a rubberband
88 // selection is ongoing.
89 return;
90 }
91
92 cancelRequest();
93
94 if (item.isNull()) {
95 // The cursor is above the viewport. If files are selected,
96 // show information regarding the selection.
97 if (m_selection.size() > 0) {
98 m_fileItem = KFileItem();
99 m_infoTimer->start();
100 }
101 } else if (item.url().isValid() && !isEqualToShownUrl(item.url())) {
102 // The cursor is above an item that is not shown currently
103 m_urlCandidate = item.url();
104 m_fileItem = item;
105 m_infoTimer->start();
106 }
107 }
108
109 bool InformationPanel::urlChanged()
110 {
111 if (!url().isValid()) {
112 return false;
113 }
114
115 if (!isVisible()) {
116 return true;
117 }
118
119 cancelRequest();
120 m_selection.clear();
121
122 if (!isEqualToShownUrl(url())) {
123 m_shownUrl = url();
124 m_fileItem = KFileItem();
125
126 // Update the content with a delay. This gives
127 // the directory lister the chance to show the content
128 // before expensive operations are done to show
129 // meta information.
130 m_urlChangedTimer->start();
131 }
132
133 return true;
134 }
135
136 void InformationPanel::showEvent(QShowEvent* event)
137 {
138 Panel::showEvent(event);
139 if (!event->spontaneous()) {
140 if (!m_initialized) {
141 // do a delayed initialization so that no performance
142 // penalty is given when Dolphin is started with a closed
143 // Information Panel
144 init();
145 }
146
147 m_shownUrl = url();
148 showItemInfo();
149 }
150 }
151
152 void InformationPanel::resizeEvent(QResizeEvent* event)
153 {
154 if (isVisible()) {
155 m_urlCandidate = m_shownUrl;
156 m_infoTimer->start();
157 }
158 Panel::resizeEvent(event);
159 }
160
161 void InformationPanel::contextMenuEvent(QContextMenuEvent* event)
162 {
163 // TODO: Move code from InformationPanelContent::configureSettings() here
164 m_content->configureSettings(customContextMenuActions());
165 Panel::contextMenuEvent(event);
166 }
167
168 void InformationPanel::showItemInfo()
169 {
170 if (!isVisible()) {
171 return;
172 }
173
174 cancelRequest();
175
176 if (m_fileItem.isNull() && (m_selection.count() > 1)) {
177 // The information for a selection of items should be shown
178 m_content->showItems(m_selection);
179 } else {
180 // The information for exactly one item should be shown
181 KFileItem item;
182 if (!m_fileItem.isNull()) {
183 item = m_fileItem;
184 } else if (!m_selection.isEmpty()) {
185 Q_ASSERT(m_selection.count() == 1);
186 item = m_selection.first();
187 }
188
189 if (item.isNull()) {
190 // No item is hovered and no selection has been done: provide
191 // an item for the currently shown directory.
192 m_folderStatJob = KIO::stat(url(), KIO::HideProgressInfo);
193 connect(m_folderStatJob, SIGNAL(result(KJob*)),
194 this, SLOT(slotFolderStatFinished(KJob*)));
195 } else {
196 m_content->showItem(item);
197 }
198 }
199 }
200
201 void InformationPanel::slotFolderStatFinished(KJob* job)
202 {
203 m_folderStatJob = 0;
204 const KIO::UDSEntry entry = static_cast<KIO::StatJob*>(job)->statResult();
205 m_content->showItem(KFileItem(entry, m_shownUrl));
206 }
207
208 void InformationPanel::slotInfoTimeout()
209 {
210 m_shownUrl = m_urlCandidate;
211 m_urlCandidate.clear();
212 showItemInfo();
213 }
214
215 void InformationPanel::reset()
216 {
217 if (m_invalidUrlCandidate == m_shownUrl) {
218 m_invalidUrlCandidate = KUrl();
219
220 // The current URL is still invalid. Reset
221 // the content to show the directory URL.
222 m_selection.clear();
223 m_shownUrl = url();
224 m_fileItem = KFileItem();
225 showItemInfo();
226 }
227 }
228
229 void InformationPanel::slotFileRenamed(const QString& source, const QString& dest)
230 {
231 if (m_shownUrl == KUrl(source)) {
232 m_shownUrl = KUrl(dest);
233 m_fileItem = KFileItem(KFileItem::Unknown, KFileItem::Unknown, m_shownUrl);
234
235 if ((m_selection.count() == 1) && (m_selection[0].url() == KUrl(source))) {
236 m_selection[0] = m_fileItem;
237 // Implementation note: Updating the selection is only required if exactly one
238 // item is selected, as the name of the item is shown. If this should change
239 // in future: Before parsing the whole selection take care to test possible
240 // performance bottlenecks when renaming several hundreds of files.
241 }
242
243 showItemInfo();
244 }
245 }
246
247 void InformationPanel::slotFilesAdded(const QString& directory)
248 {
249 if (m_shownUrl == KUrl(directory)) {
250 // If the 'trash' icon changes because the trash has been emptied or got filled,
251 // the signal filesAdded("trash:/") will be emitted.
252 KFileItem item(KFileItem::Unknown, KFileItem::Unknown, KUrl(directory));
253 requestDelayedItemInfo(item);
254 }
255 }
256
257 void InformationPanel::slotFilesChanged(const QStringList& files)
258 {
259 foreach (const QString& fileName, files) {
260 if (m_shownUrl == KUrl(fileName)) {
261 showItemInfo();
262 break;
263 }
264 }
265 }
266
267 void InformationPanel::slotFilesRemoved(const QStringList& files)
268 {
269 foreach (const QString& fileName, files) {
270 if (m_shownUrl == KUrl(fileName)) {
271 // the currently shown item has been removed, show
272 // the parent directory as fallback
273 markUrlAsInvalid();
274 break;
275 }
276 }
277 }
278
279 void InformationPanel::slotEnteredDirectory(const QString& directory)
280 {
281 if (m_shownUrl == KUrl(directory)) {
282 KFileItem item(KFileItem::Unknown, KFileItem::Unknown, KUrl(directory));
283 requestDelayedItemInfo(item);
284 }
285 }
286
287 void InformationPanel::slotLeftDirectory(const QString& directory)
288 {
289 if (m_shownUrl == KUrl(directory)) {
290 // The signal 'leftDirectory' is also emitted when a media
291 // has been unmounted. In this case no directory change will be
292 // done in Dolphin, but the Information Panel must be updated to
293 // indicate an invalid directory.
294 markUrlAsInvalid();
295 }
296 }
297
298 void InformationPanel::cancelRequest()
299 {
300 delete m_folderStatJob;
301 m_folderStatJob = 0;
302
303 m_infoTimer->stop();
304 m_resetUrlTimer->stop();
305 // Don't reset m_urlChangedTimer. As it is assured that the timeout of m_urlChangedTimer
306 // has the smallest interval (see init()), it is not possible that the exceeded timer
307 // would overwrite an information provided by a selection or hovering.
308
309 m_invalidUrlCandidate.clear();
310 m_urlCandidate.clear();
311 }
312
313 bool InformationPanel::isEqualToShownUrl(const KUrl& url) const
314 {
315 return m_shownUrl.equals(url, KUrl::CompareWithoutTrailingSlash);
316 }
317
318 void InformationPanel::markUrlAsInvalid()
319 {
320 m_invalidUrlCandidate = m_shownUrl;
321 m_resetUrlTimer->start();
322 }
323
324 void InformationPanel::init()
325 {
326 m_infoTimer = new QTimer(this);
327 m_infoTimer->setInterval(300);
328 m_infoTimer->setSingleShot(true);
329 connect(m_infoTimer, SIGNAL(timeout()),
330 this, SLOT(slotInfoTimeout()));
331
332 m_urlChangedTimer = new QTimer(this);
333 m_urlChangedTimer->setInterval(200);
334 m_urlChangedTimer->setSingleShot(true);
335 connect(m_urlChangedTimer, SIGNAL(timeout()),
336 this, SLOT(showItemInfo()));
337
338 m_resetUrlTimer = new QTimer(this);
339 m_resetUrlTimer->setInterval(1000);
340 m_resetUrlTimer->setSingleShot(true);
341 connect(m_resetUrlTimer, SIGNAL(timeout()),
342 this, SLOT(reset()));
343
344 Q_ASSERT(m_urlChangedTimer->interval() < m_infoTimer->interval());
345 Q_ASSERT(m_urlChangedTimer->interval() < m_resetUrlTimer->interval());
346
347 org::kde::KDirNotify* dirNotify = new org::kde::KDirNotify(QString(), QString(),
348 QDBusConnection::sessionBus(), this);
349 connect(dirNotify, SIGNAL(FileRenamed(QString,QString)), SLOT(slotFileRenamed(QString,QString)));
350 connect(dirNotify, SIGNAL(FilesAdded(QString)), SLOT(slotFilesAdded(QString)));
351 connect(dirNotify, SIGNAL(FilesChanged(QStringList)), SLOT(slotFilesChanged(QStringList)));
352 connect(dirNotify, SIGNAL(FilesRemoved(QStringList)), SLOT(slotFilesRemoved(QStringList)));
353 connect(dirNotify, SIGNAL(enteredDirectory(QString)), SLOT(slotEnteredDirectory(QString)));
354 connect(dirNotify, SIGNAL(leftDirectory(QString)), SLOT(slotLeftDirectory(QString)));
355
356 m_content = new InformationPanelContent(this);
357 connect(m_content, SIGNAL(urlActivated(KUrl)), this, SIGNAL(urlActivated(KUrl)));
358
359 QVBoxLayout* layout = new QVBoxLayout(this);
360 layout->setContentsMargins(0, 0, 0, 0);
361 layout->addWidget(m_content);
362
363 m_initialized = true;
364 }
365
366 #include "informationpanel.moc"