File indexing completed on 2022-11-23 12:12:13

0001 /*******************************************************************
0002  * statuswidget.cpp
0003  * SPDX-FileCopyrightText: 2009, 2010 Dario Andres Rodriguez <andresbajotierra@gmail.com>
0004  *
0005  * SPDX-License-Identifier: GPL-2.0-or-later
0006  *
0007  ******************************************************************/
0008 #include "statuswidget.h"
0009 
0010 #include <QApplication>
0011 #include <QHBoxLayout>
0012 #include <QSizePolicy>
0013 
0014 #include <KBusyIndicatorWidget>
0015 
0016 StatusWidget::StatusWidget(QWidget *parent)
0017     : QStackedWidget(parent)
0018     , m_cursorStackCount(0)
0019     , m_busy(false)
0020 {
0021     setSizePolicy(QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed));
0022 
0023     // Main layout
0024     m_statusPage = new QWidget(this);
0025     m_busyPage = new QWidget(this);
0026 
0027     addWidget(m_statusPage);
0028     addWidget(m_busyPage);
0029 
0030     // Status widget
0031     m_statusLabel = new WrapLabel();
0032     m_statusLabel->setOpenExternalLinks(true);
0033     m_statusLabel->setTextFormat(Qt::RichText);
0034     // m_statusLabel->setSizePolicy(QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum));
0035 
0036     auto *statusLayout = new QHBoxLayout();
0037     statusLayout->setContentsMargins(0, 0, 0, 0);
0038     m_statusPage->setLayout(statusLayout);
0039 
0040     statusLayout->addWidget(m_statusLabel);
0041 
0042     // Busy widget
0043     m_throbberWidget = new KBusyIndicatorWidget(this);
0044     m_throbberWidget->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed));
0045 
0046     m_busyLabel = new WrapLabel();
0047     // m_busyLabel->setSizePolicy(QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum));
0048 
0049     auto *busyLayout = new QHBoxLayout();
0050     busyLayout->setContentsMargins(0, 0, 0, 0);
0051     m_busyPage->setLayout(busyLayout);
0052 
0053     busyLayout->addWidget(m_busyLabel);
0054     busyLayout->addWidget(m_throbberWidget);
0055     busyLayout->setAlignment(m_throbberWidget, Qt::AlignVCenter);
0056 }
0057 
0058 void StatusWidget::setBusy(const QString &busyMessage)
0059 {
0060     m_statusLabel->clear();
0061     m_busyLabel->setText(busyMessage);
0062     setCurrentWidget(m_busyPage);
0063     setBusyCursor();
0064     m_busy = true;
0065 }
0066 
0067 void StatusWidget::setIdle(const QString &idleMessage)
0068 {
0069     m_busyLabel->clear();
0070     m_statusLabel->setText(idleMessage);
0071     setCurrentWidget(m_statusPage);
0072     setIdleCursor();
0073     m_busy = false;
0074 }
0075 
0076 void StatusWidget::addCustomStatusWidget(QWidget *widget)
0077 {
0078     auto *statusLayout = static_cast<QHBoxLayout *>(m_statusPage->layout());
0079 
0080     statusLayout->addWidget(widget);
0081     statusLayout->setAlignment(widget, Qt::AlignVCenter);
0082     widget->setSizePolicy(QSizePolicy(QSizePolicy::Maximum, QSizePolicy::Fixed));
0083 }
0084 
0085 void StatusWidget::setBusyCursor()
0086 {
0087     QApplication::setOverrideCursor(Qt::WaitCursor);
0088     m_cursorStackCount++;
0089 }
0090 
0091 void StatusWidget::setIdleCursor()
0092 {
0093     while (m_cursorStackCount != 0) {
0094         QApplication::restoreOverrideCursor();
0095         m_cursorStackCount--;
0096     }
0097 }
0098 
0099 void StatusWidget::hideEvent(QHideEvent *)
0100 {
0101     if (m_busy) {
0102         setIdleCursor();
0103     }
0104 }
0105 
0106 void StatusWidget::showEvent(QShowEvent *)
0107 {
0108     if (m_busy) {
0109         setBusyCursor();
0110     }
0111 }