| 1 | // For license of this file, see <project-root-folder>/LICENSE.md. |
| 2 | |
| 3 | #include "gui/toolbars/statusbar.h" |
| 4 | |
| 5 | #include "gui/dialogs/formmain.h" |
| 6 | #include "gui/reusable/plaintoolbutton.h" |
| 7 | #include "gui/reusable/progressbarwithtext.h" |
| 8 | #include "gui/tabwidget.h" |
| 9 | #include "miscellaneous/iconfactory.h" |
| 10 | #include "miscellaneous/settings.h" |
| 11 | |
| 12 | #include <QLabel> |
| 13 | #include <QToolButton> |
| 14 | |
| 15 | StatusBar::StatusBar(QWidget* parent) : QStatusBar(parent) { |
| 16 | setSizeGripEnabled(false); |
| 17 | setContentsMargins(2, 0, 2, 2); |
| 18 | |
| 19 | m_barProgressFeeds = new ProgressBarWithText(this); |
| 20 | m_barProgressFeeds->setTextVisible(true); |
| 21 | m_barProgressFeeds->setFixedWidth(250); |
| 22 | m_barProgressFeeds->setVisible(false); |
| 23 | m_barProgressFeeds->setObjectName(QSL("m_barProgressFeeds")); |
| 24 | |
| 25 | m_barProgressFeedsAction = |
| 26 | new QAction(qApp->icons()->fromTheme(QSL("application-rss+xml")), tr("Feed update progress bar"), this); |
| 27 | m_barProgressFeedsAction->setObjectName(QSL("m_barProgressFeedsAction")); |
| 28 | |
| 29 | m_barProgressDownload = new ProgressBarWithText(this); |
| 30 | m_barProgressDownload->setTextVisible(true); |
| 31 | m_barProgressDownload->setFixedWidth(230); |
| 32 | m_barProgressDownload->setVisible(false); |
| 33 | m_barProgressDownload->setObjectName(QSL("m_barProgressDownload")); |
| 34 | |
| 35 | m_barProgressDownloadAction = new QAction(qApp->icons()->fromTheme(QSL("emblem-downloads"), QSL("download")), |
| 36 | tr("File download progress bar"), |
| 37 | this); |
| 38 | m_barProgressDownloadAction->setObjectName(QSL("m_barProgressDownloadAction")); |
| 39 | |
| 40 | m_barProgressDownload->installEventFilter(this); |
| 41 | } |
| 42 | |
| 43 | StatusBar::~StatusBar() { |
| 44 | clear(); |
| 45 | qDebugNN << LOGSEC_GUI "Destroying StatusBar instance."; |
| 46 | } |
| 47 | |
| 48 | QList<QAction*> StatusBar::availableActions() const { |
| 49 | QList<QAction*> actions = qApp->userActions(); |
| 50 | |
| 51 | // Now, add placeholder actions for custom stuff. |
| 52 | actions << m_barProgressDownloadAction << m_barProgressFeedsAction; |
| 53 | |
| 54 | return actions; |
| 55 | } |
| 56 | |
| 57 | QList<QAction*> StatusBar::activatedActions() const { |
| 58 | return actions(); |
| 59 | } |
| 60 | |
| 61 | void StatusBar::saveAndSetActions(const QStringList& actions) { |
| 62 | qApp->settings()->setValue(GROUP(GUI), GUI::StatusbarActions, actions.join(QSL(","))); |
| 63 | loadSpecificActions(convertActions(actions)); |
| 64 | } |
| 65 | |
| 66 | QStringList StatusBar::defaultActions() const { |
| 67 | return QString(GUI::StatusbarActionsDef) |
| 68 | .split(',', |
| 69 | #if QT_VERSION >= 0x050F00 // Qt >= 5.15.0 |
| 70 | Qt::SplitBehaviorFlags::SkipEmptyParts); |
| 71 | #else |
| 72 | QString::SplitBehavior::SkipEmptyParts); |
| 73 | #endif |
| 74 | } |
| 75 | |
| 76 | QStringList StatusBar::savedActions() const { |
| 77 | return qApp->settings() |
| 78 | ->value(GROUP(GUI), SETTING(GUI::StatusbarActions)) |
| 79 | .toString() |
| 80 | .split(',', |
| 81 | #if QT_VERSION >= 0x050F00 // Qt >= 5.15.0 |
| 82 | Qt::SplitBehaviorFlags::SkipEmptyParts); |
| 83 | #else |
| 84 | QString::SplitBehavior::SkipEmptyParts); |
| 85 | #endif |
| 86 | } |
| 87 | |
| 88 | QList<QAction*> StatusBar::convertActions(const QStringList& actions) { |
| 89 | bool progress_visible = this->actions().contains(m_barProgressFeedsAction) && m_barProgressFeeds->isVisible(); |
| 90 | QList<QAction*> available_actions = availableActions(); |
| 91 | QList<QAction*> spec_actions; |
| 92 | |
| 93 | spec_actions.reserve(actions.size()); |
| 94 | |
| 95 | // Iterate action names and add respectable |
| 96 | // actions into the toolbar. |
| 97 | for (const QString& action_name : actions) { |
| 98 | QAction* matching_action = findMatchingAction(action_name, available_actions); |
| 99 | QAction* action_to_add; |
| 100 | QWidget* widget_to_add; |
| 101 | |
| 102 | if (matching_action == m_barProgressDownloadAction) { |
| 103 | widget_to_add = m_barProgressDownload; |
| 104 | action_to_add = m_barProgressDownloadAction; |
| 105 | widget_to_add->setVisible(false); |
| 106 | } |
| 107 | else if (matching_action == m_barProgressFeedsAction) { |
| 108 | widget_to_add = m_barProgressFeeds; |
| 109 | action_to_add = m_barProgressFeedsAction; |
| 110 | widget_to_add->setVisible(progress_visible); |
| 111 | } |
| 112 | else { |
| 113 | if (action_name == QSL(SEPARATOR_ACTION_NAME)) { |
| 114 | QLabel* lbl = new QLabel(QSL("\u2022"), this); |
| 115 | |
| 116 | widget_to_add = lbl; |
| 117 | action_to_add = new QAction(this); |
| 118 | action_to_add->setSeparator(true); |
| 119 | } |
| 120 | else if (action_name == QSL(SPACER_ACTION_NAME)) { |
| 121 | QLabel* lbl = new QLabel(QSL("\t\t"), this); |
| 122 | |
| 123 | widget_to_add = lbl; |
| 124 | action_to_add = new QAction(this); |
| 125 | action_to_add->setIcon(qApp->icons()->fromTheme(QSL("system-search"))); |
| 126 | action_to_add->setProperty("type", SPACER_ACTION_NAME); |
| 127 | action_to_add->setProperty("name", tr("Toolbar spacer")); |
| 128 | } |
| 129 | else if (matching_action != nullptr) { |
| 130 | // Add originally toolbar action. |
| 131 | auto* tool_button = new PlainToolButton(this); |
| 132 | |
| 133 | tool_button->reactOnActionChange(matching_action); |
| 134 | widget_to_add = tool_button; |
| 135 | action_to_add = matching_action; |
| 136 | connect(tool_button, &PlainToolButton::clicked, matching_action, &QAction::trigger); |
| 137 | connect(matching_action, &QAction::changed, tool_button, &PlainToolButton::reactOnSenderActionChange); |
| 138 | } |
| 139 | else { |
| 140 | action_to_add = nullptr; |
| 141 | widget_to_add = nullptr; |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | if (action_to_add != nullptr && widget_to_add != nullptr) { |
| 146 | action_to_add->setProperty("widget", QVariant::fromValue(widget_to_add)); |
| 147 | spec_actions.append(action_to_add); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | return spec_actions; |
| 152 | } |
| 153 | |
| 154 | void StatusBar::loadSpecificActions(const QList<QAction*>& actions, bool initial_load) { |
| 155 | if (initial_load) { |
| 156 | clear(); |
| 157 | |
| 158 | for (QAction* act : actions) { |
| 159 | QWidget* widget = act->property("widget").isValid() ? qvariant_cast<QWidget*>(act->property("widget")) : nullptr; |
| 160 | |
| 161 | addAction(act); |
| 162 | |
| 163 | // And also add widget. |
| 164 | if (widget != nullptr) { |
| 165 | addPermanentWidget(widget); |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | bool StatusBar::eventFilter(QObject* watched, QEvent* event) { |
| 172 | if (watched == m_barProgressDownload) { |
| 173 | if (event->type() == QEvent::Type::MouseButtonPress) { |
| 174 | qApp->mainForm()->tabWidget()->showDownloadManager(); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | return false; |
| 179 | } |
| 180 | |
| 181 | void StatusBar::clear() { |
| 182 | while (!actions().isEmpty()) { |
| 183 | QAction* act = actions().at(0); |
| 184 | QWidget* widget = |
| 185 | act->property("widget").isValid() ? static_cast<QWidget*>(act->property("widget").value<void*>()) : nullptr; |
| 186 | |
| 187 | if (widget != nullptr) { |
| 188 | removeWidget(widget); |
| 189 | |
| 190 | widget->setParent(qApp->mainFormWidget()); |
| 191 | widget->setVisible(false); |
| 192 | } |
| 193 | |
| 194 | removeAction(act); |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | void StatusBar::showProgressFeeds(int progress, const QString& label) { |
| 199 | if (actions().contains(m_barProgressFeedsAction)) { |
| 200 | m_barProgressFeeds->setVisible(true); |
| 201 | m_barProgressFeeds->setFormat(label); |
| 202 | |
| 203 | if (progress < 0) { |
| 204 | m_barProgressFeeds->setRange(0, 0); |
| 205 | } |
| 206 | else { |
| 207 | m_barProgressFeeds->setRange(0, 100); |
| 208 | m_barProgressFeeds->setValue(progress); |
| 209 | } |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | void StatusBar::clearProgressFeeds() { |
| 214 | m_barProgressFeeds->setVisible(false); |
| 215 | } |
| 216 | |
| 217 | void StatusBar::showProgressDownload(int progress, const QString& tooltip) { |
| 218 | if (actions().contains(m_barProgressDownloadAction)) { |
| 219 | m_barProgressDownload->setVisible(true); |
| 220 | m_barProgressDownload->setFormat(tooltip); |
| 221 | m_barProgressDownload->setToolTip(tooltip); |
| 222 | |
| 223 | if (progress < 0) { |
| 224 | m_barProgressDownload->setRange(0, 0); |
| 225 | } |
| 226 | else { |
| 227 | m_barProgressDownload->setRange(0, 100); |
| 228 | m_barProgressDownload->setValue(progress); |
| 229 | } |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | void StatusBar::clearProgressDownload() { |
| 234 | m_barProgressDownload->setVisible(false); |
| 235 | m_barProgressDownload->setValue(0); |
| 236 | } |