]>
cloud.milkyroute.net Git - dolphin.git/blob - src/panels/information/informationpanel.cpp
2 * SPDX-FileCopyrightText: 2006-2009 Peter Penz <peter.penz19@gmail.com>
4 * SPDX-License-Identifier: GPL-2.0-or-later
7 #include "informationpanel.h"
9 #include "informationpanelcontent.h"
13 #include <KJobWidgets>
14 #include <KLocalizedString>
16 #include <Baloo/FileMetaDataWidget>
18 #include <QApplication>
22 #include <QVBoxLayout>
24 #include "dolphin_informationpanelsettings.h"
26 InformationPanel::InformationPanel(QWidget
*parent
)
28 , m_initialized(false)
29 , m_infoTimer(nullptr)
30 , m_urlChangedTimer(nullptr)
31 , m_resetUrlTimer(nullptr)
34 , m_invalidUrlCandidate()
37 , m_folderStatJob(nullptr)
42 InformationPanel::~InformationPanel()
46 void InformationPanel::setSelection(const KFileItemList
&selection
)
48 m_selection
= selection
;
54 const int count
= selection
.count();
56 if (!isEqualToShownUrl(url())) {
62 if ((count
== 1) && !selection
.first().url().isEmpty()) {
63 m_urlCandidate
= selection
.first().url();
69 void InformationPanel::requestDelayedItemInfo(const KFileItem
&item
)
75 if (QApplication::mouseButtons() & Qt::LeftButton
) {
76 // Ignore the request of an item information when a rubberband
77 // selection is ongoing.
82 m_hoveredItem
= KFileItem();
92 bool InformationPanel::urlChanged()
94 if (!url().isValid()) {
105 if (!isEqualToShownUrl(url())) {
108 // Update the content with a delay. This gives
109 // the directory lister the chance to show the content
110 // before expensive operations are done to show
112 m_urlChangedTimer
->start();
118 void InformationPanel::showEvent(QShowEvent
*event
)
120 Panel::showEvent(event
);
121 if (!event
->spontaneous()) {
122 if (!m_initialized
) {
123 // do a delayed initialization so that no performance
124 // penalty is given when Dolphin is started with a closed
134 void InformationPanel::resizeEvent(QResizeEvent
*event
)
137 m_urlCandidate
= m_shownUrl
;
138 m_infoTimer
->start();
140 Panel::resizeEvent(event
);
143 void InformationPanel::contextMenuEvent(QContextMenuEvent
*event
)
145 showContextMenu(event
->globalPos());
146 Panel::contextMenuEvent(event
);
149 void InformationPanel::showContextMenu(const QPoint
&pos
)
153 QAction
*previewAction
= popup
.addAction(i18nc("@action:inmenu", "Preview"));
154 previewAction
->setIcon(QIcon::fromTheme(QStringLiteral("view-preview")));
155 previewAction
->setCheckable(true);
156 previewAction
->setChecked(InformationPanelSettings::previewsShown());
158 QAction
*previewAutoPlayAction
= popup
.addAction(i18nc("@action:inmenu", "Auto-Play media files"));
159 previewAutoPlayAction
->setIcon(QIcon::fromTheme(QStringLiteral("media-playback-start")));
160 previewAutoPlayAction
->setCheckable(true);
161 previewAutoPlayAction
->setChecked(InformationPanelSettings::previewsAutoPlay());
163 QAction
*configureAction
= popup
.addAction(i18nc("@action:inmenu", "Configure..."));
164 configureAction
->setIcon(QIcon::fromTheme(QStringLiteral("configure")));
165 if (m_inConfigurationMode
) {
166 configureAction
->setEnabled(false);
169 QAction
*dateformatAction
= popup
.addAction(i18nc("@action:inmenu", "Condensed Date"));
170 dateformatAction
->setIcon(QIcon::fromTheme(QStringLiteral("change-date-symbolic")));
171 dateformatAction
->setCheckable(true);
172 dateformatAction
->setChecked(InformationPanelSettings::dateFormat() == static_cast<int>(Baloo::DateFormats::ShortFormat
));
174 popup
.addSeparator();
175 const auto actions
= customContextMenuActions();
176 for (QAction
*action
: actions
) {
177 popup
.addAction(action
);
180 // Open the popup and adjust the settings for the
182 QAction
*action
= popup
.exec(pos
);
187 const bool isChecked
= action
->isChecked();
188 if (action
== previewAction
) {
189 InformationPanelSettings::setPreviewsShown(isChecked
);
190 m_content
->refreshPreview();
191 } else if (action
== configureAction
) {
192 m_inConfigurationMode
= true;
193 m_content
->configureShownProperties();
195 if (action
== dateformatAction
) {
196 int dateFormat
= static_cast<int>(isChecked
? Baloo::DateFormats::ShortFormat
: Baloo::DateFormats::LongFormat
);
198 InformationPanelSettings::setDateFormat(dateFormat
);
199 m_content
->refreshMetaData();
200 } else if (action
== previewAutoPlayAction
) {
201 InformationPanelSettings::setPreviewsAutoPlay(isChecked
);
202 m_content
->setPreviewAutoPlay(isChecked
);
206 void InformationPanel::showItemInfo()
213 //qDebug() << "showItemInfo" << m_fileItem;
215 if (m_hoveredItem
.isNull() && (m_selection
.count() > 1)) {
216 // The information for a selection of items should be shown
217 m_content
->showItems(m_selection
);
219 // The information for exactly one item should be shown
221 if (!m_hoveredItem
.isNull()) {
222 item
= m_hoveredItem
;
223 } else if (!m_selection
.isEmpty()) {
224 Q_ASSERT(m_selection
.count() == 1);
225 item
= m_selection
.first();
228 if (!item
.isNull()) {
229 m_shownUrl
= item
.url();
230 m_content
->showItem(item
);
234 // No item is hovered and no selection has been done: provide
235 // an item for the currently shown directory.
237 m_folderStatJob
= KIO::statDetails(m_shownUrl
, KIO::StatJob::SourceSide
, KIO::StatDefaultDetails
| KIO::StatRecursiveSize
, KIO::HideProgressInfo
);
238 if (m_folderStatJob
->uiDelegate()) {
239 KJobWidgets::setWindow(m_folderStatJob
, this);
241 connect(m_folderStatJob
, &KIO::Job::result
, this, &InformationPanel::slotFolderStatFinished
);
245 void InformationPanel::slotFolderStatFinished(KJob
*job
)
247 m_folderStatJob
= nullptr;
248 const KIO::UDSEntry entry
= static_cast<KIO::StatJob
*>(job
)->statResult();
249 m_content
->showItem(KFileItem(entry
, m_shownUrl
));
252 void InformationPanel::slotInfoTimeout()
254 m_shownUrl
= m_urlCandidate
;
255 m_urlCandidate
.clear();
259 void InformationPanel::reset()
261 if (m_invalidUrlCandidate
== m_shownUrl
) {
262 m_invalidUrlCandidate
= QUrl();
264 // The current URL is still invalid. Reset
265 // the content to show the directory URL.
272 void InformationPanel::slotFileRenamed(const QString
&source
, const QString
&dest
)
274 auto sourceUrl
= QUrl::fromUserInput(source
);
275 if (m_shownUrl
== sourceUrl
) {
276 auto destUrl
= QUrl::fromUserInput(dest
);
278 if ((m_selection
.count() == 1) && (m_selection
[0].url() == sourceUrl
)) {
279 m_selection
[0] = KFileItem(destUrl
);
280 // Implementation note: Updating the selection is only required if exactly one
281 // item is selected, as the name of the item is shown. If this should change
282 // in future: Before parsing the whole selection take care to test possible
283 // performance bottlenecks when renaming several hundreds of files.
285 m_hoveredItem
= KFileItem(destUrl
);
292 void InformationPanel::slotFilesAdded(const QString
&directory
)
294 if (m_shownUrl
== QUrl::fromUserInput(directory
)) {
295 // If the 'trash' icon changes because the trash has been emptied or got filled,
296 // the signal filesAdded("trash:/") will be emitted.
297 requestDelayedItemInfo(KFileItem());
301 void InformationPanel::slotFilesItemChanged(const KFileItemList
&changedFileItems
)
303 const auto item
= changedFileItems
.findByUrl(m_shownUrl
);
304 if (!item
.isNull()) {
309 void InformationPanel::slotFilesChanged(const QStringList
&files
)
311 for (const QString
&fileName
: files
) {
312 if (m_shownUrl
== QUrl::fromUserInput(fileName
)) {
319 void InformationPanel::slotFilesRemoved(const QStringList
&files
)
321 for (const QString
&fileName
: files
) {
322 if (m_shownUrl
== QUrl::fromUserInput(fileName
)) {
323 // the currently shown item has been removed, show
324 // the parent directory as fallback
331 void InformationPanel::slotEnteredDirectory(const QString
&directory
)
336 void InformationPanel::slotLeftDirectory(const QString
&directory
)
338 if (m_shownUrl
== QUrl::fromUserInput(directory
)) {
339 // The signal 'leftDirectory' is also emitted when a media
340 // has been unmounted. In this case no directory change will be
341 // done in Dolphin, but the Information Panel must be updated to
342 // indicate an invalid directory.
347 void InformationPanel::cancelRequest()
349 delete m_folderStatJob
;
350 m_folderStatJob
= nullptr;
353 m_resetUrlTimer
->stop();
354 // Don't reset m_urlChangedTimer. As it is assured that the timeout of m_urlChangedTimer
355 // has the smallest interval (see init()), it is not possible that the exceeded timer
356 // would overwrite an information provided by a selection or hovering.
358 m_invalidUrlCandidate
.clear();
359 m_urlCandidate
.clear();
362 bool InformationPanel::isEqualToShownUrl(const QUrl
&url
) const
364 return m_shownUrl
.matches(url
, QUrl::StripTrailingSlash
);
367 void InformationPanel::markUrlAsInvalid()
369 m_invalidUrlCandidate
= m_shownUrl
;
370 m_resetUrlTimer
->start();
373 void InformationPanel::init()
375 m_infoTimer
= new QTimer(this);
376 m_infoTimer
->setInterval(300);
377 m_infoTimer
->setSingleShot(true);
378 connect(m_infoTimer
, &QTimer::timeout
, this, &InformationPanel::slotInfoTimeout
);
380 m_urlChangedTimer
= new QTimer(this);
381 m_urlChangedTimer
->setInterval(200);
382 m_urlChangedTimer
->setSingleShot(true);
383 connect(m_urlChangedTimer
, &QTimer::timeout
, this, &InformationPanel::showItemInfo
);
385 m_resetUrlTimer
= new QTimer(this);
386 m_resetUrlTimer
->setInterval(1000);
387 m_resetUrlTimer
->setSingleShot(true);
388 connect(m_resetUrlTimer
, &QTimer::timeout
, this, &InformationPanel::reset
);
390 Q_ASSERT(m_urlChangedTimer
->interval() < m_infoTimer
->interval());
391 Q_ASSERT(m_urlChangedTimer
->interval() < m_resetUrlTimer
->interval());
393 org::kde::KDirNotify
*dirNotify
= new org::kde::KDirNotify(QString(), QString(), QDBusConnection::sessionBus(), this);
394 connect(dirNotify
, &OrgKdeKDirNotifyInterface::FileRenamed
, this, &InformationPanel::slotFileRenamed
);
395 connect(dirNotify
, &OrgKdeKDirNotifyInterface::FilesAdded
, this, &InformationPanel::slotFilesAdded
);
396 connect(dirNotify
, &OrgKdeKDirNotifyInterface::FilesChanged
, this, &InformationPanel::slotFilesChanged
);
397 connect(dirNotify
, &OrgKdeKDirNotifyInterface::FilesRemoved
, this, &InformationPanel::slotFilesRemoved
);
398 connect(dirNotify
, &OrgKdeKDirNotifyInterface::enteredDirectory
, this, &InformationPanel::slotEnteredDirectory
);
399 connect(dirNotify
, &OrgKdeKDirNotifyInterface::leftDirectory
, this, &InformationPanel::slotLeftDirectory
);
401 m_content
= new InformationPanelContent(this);
402 connect(m_content
, &InformationPanelContent::urlActivated
, this, &InformationPanel::urlActivated
);
403 connect(m_content
, &InformationPanelContent::configurationFinished
, this, [this]() {
404 m_inConfigurationMode
= false;
406 connect(m_content
, &InformationPanelContent::contextMenuRequested
, this, &InformationPanel::showContextMenu
);
408 QVBoxLayout
*layout
= new QVBoxLayout(this);
409 layout
->setContentsMargins(0, 0, 0, 0);
410 layout
->addWidget(m_content
);
412 m_initialized
= true;