| 1 | // For license of this file, see <project-root-folder>/LICENSE.md. |
| 2 | |
| 3 | #include "gui/tabwidget.h" |
| 4 | |
| 5 | #include "definitions/definitions.h" |
| 6 | #include "gui/dialogs/formmain.h" |
| 7 | #include "gui/feedmessageviewer.h" |
| 8 | #include "gui/feedsview.h" |
| 9 | #include "gui/messagepreviewer.h" |
| 10 | #include "gui/messagesview.h" |
| 11 | #include "gui/reusable/plaintoolbutton.h" |
| 12 | #include "gui/tabbar.h" |
| 13 | #include "gui/webbrowser.h" |
| 14 | #include "miscellaneous/application.h" |
| 15 | #include "miscellaneous/iconfactory.h" |
| 16 | #include "miscellaneous/settings.h" |
| 17 | #include "miscellaneous/textfactory.h" |
| 18 | |
| 19 | #if defined(ENABLE_MEDIAPLAYER) |
| 20 | #include "gui/mediaplayer/mediaplayer.h" |
| 21 | #endif |
| 22 | |
| 23 | #include <QMenu> |
| 24 | #include <QTimer> |
| 25 | #include <QToolButton> |
| 26 | |
| 27 | TabWidget::TabWidget(QWidget* parent) : QTabWidget(parent), m_menuMain(nullptr) { |
| 28 | setTabBar(new TabBar(this)); |
| 29 | setupMainMenuButton(); |
| 30 | initializeTabs(); |
| 31 | createConnections(); |
| 32 | updateAppearance(); |
| 33 | } |
| 34 | |
| 35 | TabWidget::~TabWidget() { |
| 36 | qDebugNN << LOGSEC_GUI << "Destroying TabWidget instance."; |
| 37 | } |
| 38 | |
| 39 | void TabWidget::setupMainMenuButton() { |
| 40 | m_btnMainMenu = new PlainToolButton(this); |
| 41 | m_btnMainMenu->setAutoRaise(true); |
| 42 | m_btnMainMenu->setPadding(3); |
| 43 | m_btnMainMenu->setToolTip(tr("Displays main menu.")); |
| 44 | m_btnMainMenu->setIcon(qApp->icons()->fromTheme(QSL("go-home"))); |
| 45 | m_btnMainMenu->setPopupMode(QToolButton::ToolButtonPopupMode::InstantPopup); |
| 46 | |
| 47 | setCornerWidget(m_btnMainMenu, Qt::Corner::TopLeftCorner); |
| 48 | |
| 49 | connect(m_btnMainMenu, &PlainToolButton::clicked, this, &TabWidget::openMainMenu); |
| 50 | } |
| 51 | |
| 52 | void TabWidget::openMainMenu() { |
| 53 | if (m_menuMain == nullptr) { |
| 54 | m_menuMain = new QMenu(tr("Main menu"), this); |
| 55 | m_menuMain->addMenu(qApp->mainForm()->m_ui->m_menuFile); |
| 56 | m_menuMain->addMenu(qApp->mainForm()->m_ui->m_menuView); |
| 57 | m_menuMain->addMenu(qApp->mainForm()->m_ui->m_menuAccounts); |
| 58 | m_menuMain->addMenu(qApp->mainForm()->m_ui->m_menuFeeds); |
| 59 | m_menuMain->addMenu(qApp->mainForm()->m_ui->m_menuMessages); |
| 60 | m_menuMain->addMenu(qApp->mainForm()->m_ui->m_menuWebBrowserTabs); |
| 61 | m_menuMain->addMenu(qApp->mainForm()->m_ui->m_menuTools); |
| 62 | m_menuMain->addMenu(qApp->mainForm()->m_ui->m_menuHelp); |
| 63 | } |
| 64 | |
| 65 | QPoint button_position = m_btnMainMenu->pos(); |
| 66 | const QSize target_size = m_btnMainMenu->size() / 2.0; |
| 67 | |
| 68 | button_position.setX(button_position.x() + target_size.width()); |
| 69 | button_position.setY(button_position.y() + target_size.height()); |
| 70 | m_menuMain->exec(mapToGlobal(button_position)); |
| 71 | } |
| 72 | |
| 73 | void TabWidget::showDownloadManager() { |
| 74 | for (int i = 0; i < count(); i++) { |
| 75 | if (widget(i)->metaObject()->className() == QSL("DownloadManager")) { |
| 76 | setCurrentIndex(i); |
| 77 | return; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | // Download manager is not opened. Create tab with it. |
| 82 | qApp->downloadManager()->setParent(this); |
| 83 | addTab(qApp->downloadManager(), |
| 84 | qApp->icons()->fromTheme(QSL("emblem-downloads"), QSL("download")), |
| 85 | tr("Downloads"), |
| 86 | TabBar::TabType::DownloadManager); |
| 87 | setCurrentIndex(count() - 1); |
| 88 | } |
| 89 | |
| 90 | void TabWidget::checkCornerButtonVisibility() { |
| 91 | const bool should_be_visible = count() > 1 || !tabBarAutoHide(); |
| 92 | |
| 93 | /* |
| 94 | if (should_be_visible) { |
| 95 | setCornerWidget(m_btnMainMenu, Qt::Corner::TopLeftCorner); |
| 96 | } |
| 97 | else { |
| 98 | setCornerWidget(nullptr, Qt::Corner::TopLeftCorner); |
| 99 | setCornerWidget(nullptr, Qt::Corner::TopRightCorner); |
| 100 | } |
| 101 | */ |
| 102 | |
| 103 | m_btnMainMenu->setVisible(should_be_visible); |
| 104 | // tabBar()->setVisible(should_be_visible); |
| 105 | } |
| 106 | |
| 107 | void TabWidget::updateAppearance() { |
| 108 | setTabBarAutoHide(qApp->settings()->value(GROUP(GUI), SETTING(GUI::HideTabBarIfOnlyOneTab)).toBool()); |
| 109 | } |
| 110 | |
| 111 | void TabWidget::tabInserted(int index) { |
| 112 | QTabWidget::tabInserted(index); |
| 113 | checkCornerButtonVisibility(); |
| 114 | const int count_of_tabs = count(); |
| 115 | |
| 116 | if (index < count_of_tabs - 1 && count_of_tabs > 1) { |
| 117 | // New tab was inserted and the tab is not the last one. |
| 118 | fixContentsAfterMove(index, count_of_tabs - 1); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | void TabWidget::tabRemoved(int index) { |
| 123 | QTabWidget::tabRemoved(index); |
| 124 | checkCornerButtonVisibility(); |
| 125 | const int count_of_tabs = count(); |
| 126 | |
| 127 | if (index < count_of_tabs && count_of_tabs > 1) { |
| 128 | // Some tab was removed and the tab was not the last one. |
| 129 | fixContentsAfterMove(index, count_of_tabs - 1); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | void TabWidget::createConnections() { |
| 134 | connect(tabBar(), &TabBar::tabCloseRequested, this, &TabWidget::closeTab); |
| 135 | connect(tabBar(), &TabBar::tabMoved, this, &TabWidget::fixContentsAfterMove); |
| 136 | |
| 137 | connect(feedMessageViewer()->messagesView(), |
| 138 | &MessagesView::openSingleMessageInNewTab, |
| 139 | this, |
| 140 | &TabWidget::addSingleMessageView); |
| 141 | |
| 142 | #if defined(ENABLE_MEDIAPLAYER) |
| 143 | connect(feedMessageViewer()->messagesView(), &MessagesView::playLinkInMediaPlayer, this, [this](const QString& url) { |
| 144 | addMediaPlayer(url, true); |
| 145 | }); |
| 146 | #endif |
| 147 | } |
| 148 | |
| 149 | void TabWidget::initializeTabs() { |
| 150 | // Create widget for "Feeds" page and add it. |
| 151 | m_feedMessageViewer = new FeedMessageViewer(this); |
| 152 | const int index_of_browser = addTab(m_feedMessageViewer, QIcon(), tr("Feeds"), TabBar::TabType::FeedReader); |
| 153 | |
| 154 | setTabToolTip(index_of_browser, tr("Browse your feeds and articles")); |
| 155 | } |
| 156 | |
| 157 | void TabWidget::setupIcons() { |
| 158 | // Iterate through all tabs and update icons |
| 159 | // accordingly. |
| 160 | for (int index = 0; index < count(); index++) { |
| 161 | // Index 0 usually contains widget which displays feeds & messages. |
| 162 | if (tabBar()->tabType(index) == TabBar::TabType::FeedReader) { |
| 163 | setTabIcon(index, qApp->icons()->fromTheme(QSL("application-rss+xml"))); |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | void TabWidget::scrollUpCurrentBrowser() { |
| 169 | currentWidget()->webBrowser()->scrollUp(); |
| 170 | } |
| 171 | |
| 172 | void TabWidget::scrollDownCurrentBrowser() { |
| 173 | currentWidget()->webBrowser()->scrollDown(); |
| 174 | } |
| 175 | |
| 176 | bool TabWidget::closeTab(int index) { |
| 177 | if (tabBar()->tabType(index) == TabBar::TabType::Closable) { |
| 178 | removeTab(index, true); |
| 179 | return true; |
| 180 | } |
| 181 | else if (tabBar()->tabType(index) == TabBar::TabType::DownloadManager) { |
| 182 | removeTab(index, false); |
| 183 | return true; |
| 184 | } |
| 185 | else { |
| 186 | return false; |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | void TabWidget::closeTabWithSender() { |
| 191 | auto idx = indexOf(qobject_cast<QWidget*>(sender())); |
| 192 | |
| 193 | if (idx >= 0) { |
| 194 | closeTab(idx); |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | void TabWidget::closeAllTabsExceptCurrent() { |
| 199 | // Close tabs after active tab. |
| 200 | int index_of_active = currentIndex(); |
| 201 | |
| 202 | for (int i = count() - 1; i >= 0; i--) { |
| 203 | if (i != index_of_active) { |
| 204 | if (i < index_of_active) { |
| 205 | index_of_active--; |
| 206 | } |
| 207 | |
| 208 | closeTab(i); |
| 209 | } |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | void TabWidget::closeAllTabs() { |
| 214 | for (int i = count() - 1; i >= 0; i--) { |
| 215 | closeTab(i); |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | void TabWidget::closeCurrentTab() { |
| 220 | closeTab(currentIndex()); |
| 221 | } |
| 222 | |
| 223 | int TabWidget::addSingleMessageView(RootItem* root, const Message& message) { |
| 224 | auto* browser = new MessagePreviewer(this); |
| 225 | auto* msg_mdl = qApp->mainForm()->tabWidget()->feedMessageViewer()->messagesView()->sourceModel(); |
| 226 | |
| 227 | connect(browser, &MessagePreviewer::markMessageRead, msg_mdl, &MessagesModel::setMessageReadById); |
| 228 | connect(browser, &MessagePreviewer::markMessageImportant, msg_mdl, &MessagesModel::setMessageImportantById); |
| 229 | connect(browser, &MessagePreviewer::setMessageLabelIds, msg_mdl, &MessagesModel::setMessageLabelsById); |
| 230 | |
| 231 | int index = addTab(browser, root->fullIcon(), message.m_title, TabBar::TabType::Closable); |
| 232 | |
| 233 | QTimer::singleShot(500, browser, [browser, root, message]() { |
| 234 | browser->loadMessage(message, root); |
| 235 | }); |
| 236 | |
| 237 | return index; |
| 238 | } |
| 239 | |
| 240 | int TabWidget::addEmptyBrowser() { |
| 241 | return addBrowser(false, true); |
| 242 | } |
| 243 | |
| 244 | #if defined(ENABLE_MEDIAPLAYER) |
| 245 | int TabWidget::addMediaPlayer(const QString& url, bool make_active) { |
| 246 | // #if defined(ENABLE_MEDIAPLAYER_LIBMPV) |
| 247 | // QQuickWindow::setGraphicsApi(QSGRendererInterface::GraphicsApi::OpenGL); |
| 248 | // #endif |
| 249 | |
| 250 | auto* player = new MediaPlayer(this); |
| 251 | |
| 252 | connect(player, |
| 253 | &MediaPlayer::urlDownloadRequested, |
| 254 | qApp->downloadManager(), |
| 255 | QOverload<const QUrl&>::of(&DownloadManager::download)); |
| 256 | |
| 257 | connect(player, &MediaPlayer::closed, this, &TabWidget::closeTabWithSender); |
| 258 | |
| 259 | int index = addTab(player, |
| 260 | qApp->icons()->fromTheme(QSL("player_play"), QSL("media-playback-start")), |
| 261 | tr("Media player"), |
| 262 | TabBar::TabType::Closable); |
| 263 | |
| 264 | if (make_active) { |
| 265 | setCurrentIndex(index); |
| 266 | player->setFocus(Qt::FocusReason::OtherFocusReason); |
| 267 | } |
| 268 | |
| 269 | QTimer::singleShot(3000, player, [player, url]() { |
| 270 | player->playUrl(url); |
| 271 | }); |
| 272 | |
| 273 | return index; |
| 274 | } |
| 275 | #endif |
| 276 | |
| 277 | int TabWidget::addLinkedBrowser(const QUrl& initial_url) { |
| 278 | return addBrowser(false, false, initial_url); |
| 279 | } |
| 280 | |
| 281 | int TabWidget::addLinkedBrowser(const QString& initial_url) { |
| 282 | return addLinkedBrowser(QUrl::fromUserInput(initial_url)); |
| 283 | } |
| 284 | |
| 285 | int TabWidget::addBrowser(bool move_after_current, bool make_active, WebBrowser* browser) { |
| 286 | int final_index; |
| 287 | QString browser_tab_name = tr("Web browser"); |
| 288 | |
| 289 | #if defined(Q_OS_MACOSOS) |
| 290 | browser_tab_name = browser_tab_name.prepend(QSL(" ")); |
| 291 | #endif |
| 292 | |
| 293 | if (move_after_current) { |
| 294 | // Insert web browser after current tab. |
| 295 | final_index = insertTab(currentIndex() + 1, |
| 296 | browser, |
| 297 | qApp->icons()->fromTheme(QSL("text-html")), |
| 298 | browser_tab_name, |
| 299 | TabBar::TabType::Closable); |
| 300 | } |
| 301 | else { |
| 302 | // Add new browser as the last tab. |
| 303 | final_index = |
| 304 | addTab(browser, qApp->icons()->fromTheme(QSL("text-html")), browser_tab_name, TabBar::TabType::Closable); |
| 305 | } |
| 306 | |
| 307 | // Make connections. |
| 308 | connect(browser, &WebBrowser::titleChanged, this, &TabWidget::changeTitle); |
| 309 | connect(browser, &WebBrowser::iconChanged, this, &TabWidget::changeIcon); |
| 310 | connect(browser, &WebBrowser::windowCloseRequested, this, &TabWidget::closeTabWithSender); |
| 311 | |
| 312 | // Setup the tab index. |
| 313 | browser->setIndex(final_index); |
| 314 | |
| 315 | // Make new web browser active if desired. |
| 316 | if (make_active) { |
| 317 | setCurrentIndex(final_index); |
| 318 | browser->setFocus(Qt::FocusReason::OtherFocusReason); |
| 319 | } |
| 320 | |
| 321 | return final_index; |
| 322 | } |
| 323 | |
| 324 | int TabWidget::addBrowser(bool move_after_current, bool make_active, const QUrl& initial_url) { |
| 325 | // Create new WebBrowser. |
| 326 | WebBrowser* browser = new WebBrowser(nullptr, this); |
| 327 | int index = addBrowser(move_after_current, make_active, browser); |
| 328 | |
| 329 | // Load initial web page if desired. |
| 330 | if (initial_url.isValid()) { |
| 331 | browser->loadUrl(initial_url); |
| 332 | } |
| 333 | |
| 334 | return index; |
| 335 | } |
| 336 | |
| 337 | void TabWidget::gotoNextTab() { |
| 338 | if (currentIndex() == count() - 1) { |
| 339 | setCurrentIndex(0); |
| 340 | } |
| 341 | else { |
| 342 | setCurrentIndex(currentIndex() + 1); |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | void TabWidget::gotoPreviousTab() { |
| 347 | if (currentIndex() == 0) { |
| 348 | setCurrentIndex(count() - 1); |
| 349 | } |
| 350 | else { |
| 351 | setCurrentIndex(currentIndex() - 1); |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | void TabWidget::indentTabText(int index) { |
| 356 | #if defined(Q_OS_MACOS) |
| 357 | if (tabBar()->tabType(index) != TabBar::TabType::FeedReader && !tabIcon(index).isNull()) { |
| 358 | // We have closable tab with some icon, fix the title. |
| 359 | const QString text = tabText(index); |
| 360 | |
| 361 | if (!text.startsWith(QSL(" "))) { |
| 362 | setTabText(index, QSL(" ") + text); |
| 363 | } |
| 364 | } |
| 365 | #else |
| 366 | Q_UNUSED(index) |
| 367 | #endif |
| 368 | } |
| 369 | |
| 370 | void TabWidget::removeTab(int index, bool clear_from_memory) { |
| 371 | if (clear_from_memory) { |
| 372 | widget(index)->deleteLater(); |
| 373 | } |
| 374 | |
| 375 | QTabWidget::removeTab(index); |
| 376 | } |
| 377 | |
| 378 | int TabWidget::addTab(TabContent* widget, const QIcon& icon, const QString& label, TabBar::TabType type) { |
| 379 | const int index = QTabWidget::addTab(widget, icon, TextFactory::shorten(label)); |
| 380 | |
| 381 | tabBar()->setTabType(index, type); |
| 382 | indentTabText(index); |
| 383 | return index; |
| 384 | } |
| 385 | |
| 386 | int TabWidget::addTab(TabContent* widget, const QString& label, TabBar::TabType type) { |
| 387 | const int index = QTabWidget::addTab(widget, TextFactory::shorten(label)); |
| 388 | |
| 389 | tabBar()->setTabType(index, type); |
| 390 | indentTabText(index); |
| 391 | return index; |
| 392 | } |
| 393 | |
| 394 | int TabWidget::insertTab(int index, QWidget* widget, const QIcon& icon, const QString& label, TabBar::TabType type) { |
| 395 | const int tab_index = QTabWidget::insertTab(index, widget, icon, label); |
| 396 | |
| 397 | tabBar()->setTabType(tab_index, type); |
| 398 | indentTabText(index); |
| 399 | return tab_index; |
| 400 | } |
| 401 | |
| 402 | int TabWidget::insertTab(int index, QWidget* widget, const QString& label, TabBar::TabType type) { |
| 403 | const int tab_index = QTabWidget::insertTab(index, widget, label); |
| 404 | |
| 405 | tabBar()->setTabType(tab_index, type); |
| 406 | indentTabText(index); |
| 407 | return tab_index; |
| 408 | } |
| 409 | |
| 410 | void TabWidget::changeIcon(int index, const QIcon& new_icon) { |
| 411 | setTabIcon(index, new_icon); |
| 412 | indentTabText(index); |
| 413 | } |
| 414 | |
| 415 | void TabWidget::changeTitle(int index, const QString& new_title) { |
| 416 | setTabText(index, TextFactory::shorten(new_title)); |
| 417 | setTabToolTip(index, TextFactory::shorten(new_title)); |
| 418 | indentTabText(index); |
| 419 | } |
| 420 | |
| 421 | void TabWidget::fixContentsAfterMove(int from, int to) { |
| 422 | from = qMin(from, to); |
| 423 | to = qMax(from, to); |
| 424 | |
| 425 | for (; from <= to; from++) { |
| 426 | auto* content = widget(from); |
| 427 | |
| 428 | content->setIndex(from); |
| 429 | } |
| 430 | } |