File indexing completed on 2022-09-27 16:29:09

0001 /**
0002  * SPDX-FileCopyrightText: (C) 2003 by Sébastien Laoût <slaout@linux62.org>
0003  * SPDX-License-Identifier: GPL-2.0-or-later
0004  */
0005 
0006 #include "basketstatusbar.h"
0007 
0008 #include <QLocale>
0009 #include <QStatusBar>
0010 
0011 #include <QLabel>
0012 #include <QtCore/QObject>
0013 #include <QtGui/QMouseEvent>
0014 #include <QtGui/QPixmap>
0015 
0016 #include <KIconLoader>
0017 #include <KLocalizedString>
0018 #include <KParts/StatusBarExtension>
0019 
0020 #include "basketscene.h"
0021 #include "bnpview.h"
0022 #include "global.h"
0023 #include "tools.h"
0024 
0025 BasketStatusBar::BasketStatusBar(QStatusBar *bar)
0026     : m_bar(bar)
0027     , m_extension(nullptr)
0028     , m_selectionStatus(nullptr)
0029     , m_lockStatus(nullptr)
0030     , m_basketStatus(nullptr)
0031     , m_savedStatus(nullptr)
0032 {
0033 }
0034 
0035 BasketStatusBar::BasketStatusBar(KParts::StatusBarExtension *extension)
0036     : m_bar(nullptr)
0037     , m_extension(extension)
0038     , m_selectionStatus(nullptr)
0039     , m_lockStatus(nullptr)
0040     , m_basketStatus(nullptr)
0041     , m_savedStatus(nullptr)
0042 {
0043 }
0044 
0045 BasketStatusBar::~BasketStatusBar()
0046 {
0047     // delete m_extension;
0048 }
0049 
0050 QStatusBar *BasketStatusBar::statusBar() const
0051 {
0052     if (m_extension != nullptr) {
0053         return m_extension->statusBar();
0054     }
0055 
0056     return m_bar;
0057 }
0058 
0059 void BasketStatusBar::addWidget(QWidget *widget, int stretch, bool permanent)
0060 {
0061     if (m_extension != nullptr) {
0062         m_extension->addStatusBarItem(widget, stretch, permanent);
0063     } else if (permanent) {
0064         m_bar->addPermanentWidget(widget, stretch);
0065     } else {
0066         m_bar->addWidget(widget, stretch);
0067     }
0068 }
0069 
0070 void BasketStatusBar::setupStatusBar()
0071 {
0072     QWidget *parent = statusBar();
0073     QObjectList lst = parent->findChildren<QObject *>(QStringLiteral("KRSqueezedTextLabel"));
0074 
0075     // Tools::printChildren(parent);
0076     if (lst.count() == 0) {
0077         m_basketStatus = new QLabel(parent);
0078         QSizePolicy policy(QSizePolicy::Ignored, QSizePolicy::Ignored);
0079         policy.setHorizontalStretch(0);
0080         policy.setVerticalStretch(0);
0081         policy.setHeightForWidth(false);
0082         m_basketStatus->setSizePolicy(policy);
0083         addWidget(m_basketStatus, 1, false); // Fit all extra space and is hiddable
0084     } else {
0085         m_basketStatus = qobject_cast<QLabel *>(lst.at(0));
0086     }
0087     lst.clear();
0088 
0089     m_selectionStatus = new QLabel(i18n("Loading..."), parent);
0090     addWidget(m_selectionStatus, 0, true);
0091 
0092     m_lockStatus = new QLabel(nullptr /*this*/);
0093     m_lockStatus->setMinimumSize(18, 18);
0094     m_lockStatus->setAlignment(Qt::AlignCenter);
0095     //  addWidget( m_lockStatus, 0, true );
0096     m_lockStatus->installEventFilter(this);
0097 
0098     m_savedStatusPixmap = QIcon::fromTheme(QStringLiteral("document-save")).pixmap(KIconLoader::SizeSmall);
0099     m_savedStatus = new QLabel(parent);
0100     m_savedStatus->setPixmap(m_savedStatusPixmap);
0101     m_savedStatus->setFixedSize(m_savedStatus->sizeHint());
0102     m_savedStatus->clear();
0103     // m_savedStatus->setPixmap(m_savedStatusIconSet.pixmap(QIconSet::Small, QIconSet::Disabled));
0104     // m_savedStatus->setEnabled(false);
0105     addWidget(m_savedStatus, 0, true);
0106     m_savedStatus->setToolTip("<p>" + i18n("Shows if there are changes that have not yet been saved."));
0107 }
0108 
0109 void BasketStatusBar::postStatusbarMessage(const QString &text)
0110 {
0111     if (statusBar() != nullptr) {
0112         statusBar()->showMessage(text, 2000);
0113     }
0114 }
0115 
0116 void BasketStatusBar::setStatusText(const QString &txt)
0117 {
0118     if (m_basketStatus != nullptr && m_basketStatus->text() != txt) {
0119         m_basketStatus->setText(txt);
0120     }
0121 }
0122 
0123 void BasketStatusBar::setStatusBarHint(const QString &hint)
0124 {
0125     if (hint.isEmpty()) {
0126         updateStatusBarHint();
0127     } else {
0128         setStatusText(hint);
0129     }
0130 }
0131 
0132 void BasketStatusBar::updateStatusBarHint()
0133 {
0134     QString message;
0135 
0136     if (Global::bnpView->currentBasket()->isDuringDrag()) {
0137         message = i18n("Ctrl+drop: copy, Shift+drop: move, Shift+Ctrl+drop: link.");
0138     }
0139     // Too much noise information:
0140     //  else if (currentBasket()->inserterShown() && currentBasket()->inserterSplit() && !currentBasket()->inserterGroup())
0141     //      message = i18n("Click to insert a note, right click for more options. Click on the right of the line to group instead of insert.");
0142     //  else if (currentBasket()->inserterShown() && currentBasket()->inserterSplit() && currentBasket()->inserterGroup())
0143     //      message = i18n("Click to group a note, right click for more options. Click on the left of the line to group instead of insert.");
0144     else if (Global::debugWindow != nullptr) {
0145         message = "DEBUG: " + Global::bnpView->currentBasket()->folderName();
0146     }
0147 
0148     setStatusText(message);
0149 }
0150 
0151 void BasketStatusBar::setLockStatus(bool isLocked)
0152 {
0153     if (m_lockStatus == nullptr) {
0154         return;
0155     }
0156 
0157     if (isLocked) {
0158         QPixmap encryptedIcon = QIcon::fromTheme(QStringLiteral("encrypted.png")).pixmap(KIconLoader::SizeSmall);
0159         m_lockStatus->setPixmap(encryptedIcon);
0160         m_lockStatus->setToolTip(i18n("<p>This basket is <b>locked</b>.<br>Click to unlock it.</p>").replace(QChar(' '), QStringLiteral("&nbsp;")));
0161     } else {
0162         m_lockStatus->clear();
0163         m_lockStatus->setToolTip(i18n("<p>This basket is <b>unlocked</b>.<br>Click to lock it.</p>").replace(QChar(' '), QStringLiteral("&nbsp;")));
0164     }
0165 }
0166 
0167 void BasketStatusBar::setSelectionStatus(const QString &s)
0168 {
0169     if (m_selectionStatus != nullptr) {
0170         m_selectionStatus->setText(s);
0171     }
0172 }
0173 
0174 void BasketStatusBar::setUnsavedStatus(bool isUnsaved)
0175 {
0176     if (m_savedStatus == nullptr) {
0177         return;
0178     }
0179 
0180     if (isUnsaved) {
0181 #if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
0182         if (m_savedStatus->pixmap(Qt::ReturnByValueConstant::ReturnByValue).isNull()) {
0183 #else
0184         if (m_savedStatus->pixmap()->isNull()) {
0185 #endif
0186             m_savedStatus->setPixmap(m_savedStatusPixmap);
0187         }
0188     } else {
0189         m_savedStatus->clear();
0190     }
0191 }
0192 
0193 bool BasketStatusBar::eventFilter(QObject *obj, QEvent *event)
0194 {
0195     if (obj == m_lockStatus && event->type() == QEvent::MouseButtonPress) {
0196         auto *mevent = dynamic_cast<QMouseEvent *>(event);
0197         if (static_cast<bool>(mevent->button() & Qt::LeftButton)) {
0198             Global::bnpView->lockBasket();
0199             return true;
0200         }
0201     }
0202     return QObject::eventFilter(obj, event); // standard event processing
0203 }