]>
cloud.milkyroute.net Git - dolphin.git/blob - src/panels/information/informationpanel.cpp
02fe3e308ae4f58bea4c56139a353c051832ef6a
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"
12 #include <KIO/StatJob>
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
)
71 if (!isVisible() || !InformationPanelSettings::showHovered()) {
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
*showHoveredAction
= popup
.addAction(i18nc("@action:inmenu", "Show item on hover"));
164 showHoveredAction
->setIcon(QIcon::fromTheme(QStringLiteral("followmouse")));
165 showHoveredAction
->setCheckable(true);
166 showHoveredAction
->setChecked(InformationPanelSettings::showHovered());
168 QAction
*configureAction
= popup
.addAction(i18nc("@action:inmenu", "Configureā¦"));
169 configureAction
->setIcon(QIcon::fromTheme(QStringLiteral("configure")));
170 if (m_inConfigurationMode
) {
171 configureAction
->setEnabled(false);
174 QAction
*dateformatAction
= popup
.addAction(i18nc("@action:inmenu", "Condensed Date"));
175 dateformatAction
->setIcon(QIcon::fromTheme(QStringLiteral("change-date-symbolic")));
176 dateformatAction
->setCheckable(true);
177 dateformatAction
->setChecked(InformationPanelSettings::dateFormat() == static_cast<int>(Baloo::DateFormats::ShortFormat
));
179 popup
.addSeparator();
180 const auto actions
= customContextMenuActions();
181 for (QAction
*action
: actions
) {
182 popup
.addAction(action
);
185 // Open the popup and adjust the settings for the
187 QAction
*action
= popup
.exec(pos
);
192 const bool isChecked
= action
->isChecked();
193 if (action
== previewAction
) {
194 InformationPanelSettings::setPreviewsShown(isChecked
);
195 m_content
->refreshPreview();
196 } else if (action
== previewAutoPlayAction
) {
197 InformationPanelSettings::setPreviewsAutoPlay(isChecked
);
198 m_content
->setPreviewAutoPlay(isChecked
);
199 } else if (action
== showHoveredAction
) {
200 InformationPanelSettings::setShowHovered(isChecked
);
202 m_hoveredItem
= KFileItem();
205 } else if (action
== configureAction
) {
206 m_inConfigurationMode
= true;
207 m_content
->configureShownProperties();
208 } else if (action
== dateformatAction
) {
209 int dateFormat
= static_cast<int>(isChecked
? Baloo::DateFormats::ShortFormat
: Baloo::DateFormats::LongFormat
);
211 InformationPanelSettings::setDateFormat(dateFormat
);
212 m_content
->refreshMetaData();
216 void InformationPanel::showItemInfo()
223 //qDebug() << "showItemInfo" << m_fileItem;
225 if (m_hoveredItem
.isNull() && (m_selection
.count() > 1)) {
226 // The information for a selection of items should be shown
227 m_content
->showItems(m_selection
);
229 // The information for exactly one item should be shown
231 if (!m_hoveredItem
.isNull() && InformationPanelSettings::showHovered()) {
232 item
= m_hoveredItem
;
233 } else if (!m_selection
.isEmpty()) {
234 Q_ASSERT(m_selection
.count() == 1);
235 item
= m_selection
.first();
238 if (!item
.isNull()) {
239 m_shownUrl
= item
.url();
240 m_content
->showItem(item
);
244 // No item is hovered and no selection has been done: provide
245 // an item for the currently shown directory.
247 m_folderStatJob
= KIO::stat(m_shownUrl
, KIO::StatJob::SourceSide
, KIO::StatDefaultDetails
| KIO::StatRecursiveSize
, KIO::HideProgressInfo
);
248 if (m_folderStatJob
->uiDelegate()) {
249 KJobWidgets::setWindow(m_folderStatJob
, this);
251 connect(m_folderStatJob
, &KIO::Job::result
, this, &InformationPanel::slotFolderStatFinished
);
255 void InformationPanel::slotFolderStatFinished(KJob
*job
)
257 m_folderStatJob
= nullptr;
258 const KIO::UDSEntry entry
= static_cast<KIO::StatJob
*>(job
)->statResult();
259 m_content
->showItem(KFileItem(entry
, m_shownUrl
));
262 void InformationPanel::slotInfoTimeout()
264 m_shownUrl
= m_urlCandidate
;
265 m_urlCandidate
.clear();
269 void InformationPanel::reset()
271 if (m_invalidUrlCandidate
== m_shownUrl
) {
272 m_invalidUrlCandidate
= QUrl();
274 // The current URL is still invalid. Reset
275 // the content to show the directory URL.
282 void InformationPanel::slotFileRenamed(const QString
&source
, const QString
&dest
)
284 auto sourceUrl
= QUrl::fromUserInput(source
);
285 if (m_shownUrl
== sourceUrl
) {
286 auto destUrl
= QUrl::fromUserInput(dest
);
288 if ((m_selection
.count() == 1) && (m_selection
[0].url() == sourceUrl
)) {
289 m_selection
[0] = KFileItem(destUrl
);
290 // Implementation note: Updating the selection is only required if exactly one
291 // item is selected, as the name of the item is shown. If this should change
292 // in future: Before parsing the whole selection take care to test possible
293 // performance bottlenecks when renaming several hundreds of files.
300 void InformationPanel::slotFilesAdded(const QString
&directory
)
302 if (m_shownUrl
== QUrl::fromUserInput(directory
)) {
303 // If the 'trash' icon changes because the trash has been emptied or got filled,
304 // the signal filesAdded("trash:/") will be emitted.
305 requestDelayedItemInfo(KFileItem());
309 void InformationPanel::slotFilesItemChanged(const KFileItemList
&changedFileItems
)
311 const auto item
= changedFileItems
.findByUrl(m_shownUrl
);
312 if (!item
.isNull()) {
317 void InformationPanel::slotFilesChanged(const QStringList
&files
)
319 for (const QString
&fileName
: files
) {
320 if (m_shownUrl
== QUrl::fromUserInput(fileName
)) {
327 void InformationPanel::slotFilesRemoved(const QStringList
&files
)
329 for (const QString
&fileName
: files
) {
330 if (m_shownUrl
== QUrl::fromUserInput(fileName
)) {
331 // the currently shown item has been removed, show
332 // the parent directory as fallback
339 void InformationPanel::slotEnteredDirectory(const QString
&directory
)
344 void InformationPanel::slotLeftDirectory(const QString
&directory
)
346 if (m_shownUrl
== QUrl::fromUserInput(directory
)) {
347 // The signal 'leftDirectory' is also emitted when a media
348 // has been unmounted. In this case no directory change will be
349 // done in Dolphin, but the Information Panel must be updated to
350 // indicate an invalid directory.
355 void InformationPanel::cancelRequest()
357 delete m_folderStatJob
;
358 m_folderStatJob
= nullptr;
361 m_resetUrlTimer
->stop();
362 // Don't reset m_urlChangedTimer. As it is assured that the timeout of m_urlChangedTimer
363 // has the smallest interval (see init()), it is not possible that the exceeded timer
364 // would overwrite an information provided by a selection or hovering.
366 m_invalidUrlCandidate
.clear();
367 m_urlCandidate
.clear();
370 bool InformationPanel::isEqualToShownUrl(const QUrl
&url
) const
372 return m_shownUrl
.matches(url
, QUrl::StripTrailingSlash
);
375 void InformationPanel::markUrlAsInvalid()
377 m_invalidUrlCandidate
= m_shownUrl
;
378 m_resetUrlTimer
->start();
381 void InformationPanel::readSettings()
384 m_content
->refreshPreview();
385 m_content
->refreshMetaData();
389 void InformationPanel::init()
391 m_infoTimer
= new QTimer(this);
392 m_infoTimer
->setInterval(300);
393 m_infoTimer
->setSingleShot(true);
394 connect(m_infoTimer
, &QTimer::timeout
, this, &InformationPanel::slotInfoTimeout
);
396 m_urlChangedTimer
= new QTimer(this);
397 m_urlChangedTimer
->setInterval(200);
398 m_urlChangedTimer
->setSingleShot(true);
399 connect(m_urlChangedTimer
, &QTimer::timeout
, this, &InformationPanel::showItemInfo
);
401 m_resetUrlTimer
= new QTimer(this);
402 m_resetUrlTimer
->setInterval(1000);
403 m_resetUrlTimer
->setSingleShot(true);
404 connect(m_resetUrlTimer
, &QTimer::timeout
, this, &InformationPanel::reset
);
406 Q_ASSERT(m_urlChangedTimer
->interval() < m_infoTimer
->interval());
407 Q_ASSERT(m_urlChangedTimer
->interval() < m_resetUrlTimer
->interval());
409 org::kde::KDirNotify
*dirNotify
= new org::kde::KDirNotify(QString(), QString(), QDBusConnection::sessionBus(), this);
410 connect(dirNotify
, &OrgKdeKDirNotifyInterface::FileRenamed
, this, &InformationPanel::slotFileRenamed
);
411 connect(dirNotify
, &OrgKdeKDirNotifyInterface::FilesAdded
, this, &InformationPanel::slotFilesAdded
);
412 connect(dirNotify
, &OrgKdeKDirNotifyInterface::FilesChanged
, this, &InformationPanel::slotFilesChanged
);
413 connect(dirNotify
, &OrgKdeKDirNotifyInterface::FilesRemoved
, this, &InformationPanel::slotFilesRemoved
);
414 connect(dirNotify
, &OrgKdeKDirNotifyInterface::enteredDirectory
, this, &InformationPanel::slotEnteredDirectory
);
415 connect(dirNotify
, &OrgKdeKDirNotifyInterface::leftDirectory
, this, &InformationPanel::slotLeftDirectory
);
417 m_content
= new InformationPanelContent(this);
418 connect(m_content
, &InformationPanelContent::urlActivated
, this, &InformationPanel::urlActivated
);
419 connect(m_content
, &InformationPanelContent::configurationFinished
, this, [this]() {
420 m_inConfigurationMode
= false;
422 connect(m_content
, &InformationPanelContent::contextMenuRequested
, this, &InformationPanel::showContextMenu
);
424 QVBoxLayout
*layout
= new QVBoxLayout(this);
425 layout
->setContentsMargins(0, 0, 0, 0);
426 layout
->addWidget(m_content
);
428 m_initialized
= true;
431 #include "moc_informationpanel.cpp"