Libksieve

sieveeditortabwidget.cpp
1/*
2 SPDX-FileCopyrightText: 2013-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6#include "sieveeditortabwidget.h"
7#include "webengine/sieveeditorhelphtmlwidget.h"
8
9#include <KLocalizedString>
10#include <KStringHandler>
11
12#include <QMenu>
13#include <QTabBar>
14#include <QUrl>
15
16using namespace KSieveUi;
17SieveEditorTabWidget::SieveEditorTabWidget(QWidget *parent)
18 : QTabWidget(parent)
19{
20 setTabsClosable(true);
21 connect(this, &SieveEditorTabWidget::tabCloseRequested, this, &SieveEditorTabWidget::slotTabCloseRequested);
22 setContextMenuPolicy(Qt::CustomContextMenu);
23 connect(this, &SieveEditorTabWidget::customContextMenuRequested, this, &SieveEditorTabWidget::slotTabContextMenuRequest);
24}
25
26SieveEditorTabWidget::~SieveEditorTabWidget() = default;
27
28void SieveEditorTabWidget::slotTabContextMenuRequest(const QPoint &pos)
29{
30 QTabBar *bar = tabBar();
31 if (count() <= 1) {
32 return;
33 }
34
35 const int indexBar = bar->tabAt(bar->mapFrom(this, pos));
36 if (indexBar == -1) {
37 return;
38 }
39 QMenu menu(this);
40
41 const bool hasMoreThanTwoTab = (count() > 1);
42
43 QAction *closeTab = nullptr;
44 if ((indexBar != 0) && hasMoreThanTwoTab) {
45 closeTab = menu.addAction(i18nc("@action:inmenu", "Close Tab"));
46 closeTab->setIcon(QIcon::fromTheme(QStringLiteral("tab-close")));
47 }
48
49 QAction *allOther = nullptr;
50 if ((indexBar == 0) || (count() > 2)) {
51 allOther = menu.addAction(i18nc("@action:inmenu", "Close All Other Tabs"));
52 allOther->setIcon(QIcon::fromTheme(QStringLiteral("tab-close-other")));
53 }
54
55 QAction *allTab = nullptr;
56 if (hasMoreThanTwoTab) {
57 allTab = menu.addAction(i18nc("@action:inmenu", "Close All Tabs"));
58 allTab->setEnabled(hasMoreThanTwoTab);
59 allTab->setIcon(QIcon::fromTheme(QStringLiteral("tab-close")));
60 }
61
62 QAction *action = menu.exec(mapToGlobal(pos));
63
64 if (action) {
65 if (action == allOther) { // Close all other tabs
66 slotCloseAllTabExcept(indexBar);
67 } else if (action == closeTab) {
68 slotCloseRequest(indexBar);
69 } else if (action == allTab) {
70 slotCloseAllTab();
71 }
72 }
73}
74
75void SieveEditorTabWidget::slotCloseRequest(int index)
76{
77 if (index != 0) {
78 removeTab(index);
79 }
80}
81
82void SieveEditorTabWidget::closeAllTabExcept(int index)
83{
84 // Don't close first tab
85 for (int i = count() - 1; i > 0; --i) {
86 if (i == index) {
87 continue;
88 }
89 removeTab(i);
90 }
91}
92
93void SieveEditorTabWidget::slotCloseAllTabExcept(int index)
94{
95 closeAllTabExcept(index);
96}
97
98void SieveEditorTabWidget::slotCloseAllTab()
99{
100 closeAllTabExcept();
101}
102
103void SieveEditorTabWidget::slotTabCloseRequested(int index)
104{
105 // Don't remove first tab.
106 if (index > 0) {
107 removeTab(index);
108 }
109}
110
111bool SieveEditorTabWidget::currentPageIsHtmlPage() const
112{
114 return page ? true : false;
115}
116
117QUrl SieveEditorTabWidget::currentHelpUrl() const
118{
120 if (page) {
121 return page->currentUrl();
122 }
123 return {};
124}
125
126QString SieveEditorTabWidget::currentHelpTitle() const
127{
129 if (page) {
130 return page->title();
131 }
132 return {};
133}
134
135void SieveEditorTabWidget::slotAddHelpPage(const QUrl &url)
136{
137 for (int i = 0; i < count(); ++i) {
139 if (page && page->currentUrl() == url) {
141 return;
142 }
143 }
144 auto htmlPage = new SieveEditorHelpHtmlWidget;
145 connect(htmlPage, &SieveEditorHelpHtmlWidget::titleChanged, this, &SieveEditorTabWidget::slotTitleChanged);
146 connect(htmlPage, &SieveEditorHelpHtmlWidget::progressIndicatorPixmapChanged, this, &SieveEditorTabWidget::slotProgressIndicatorPixmapChanged);
147 connect(htmlPage, &SieveEditorHelpHtmlWidget::loadFinished, this, &SieveEditorTabWidget::slotLoadFinished);
148 connect(htmlPage, &SieveEditorHelpHtmlWidget::copyAvailable, this, &SieveEditorTabWidget::copyAvailable);
149 htmlPage->openUrl(url);
150 const int index = addTab(htmlPage, i18n("Help"));
151 setCurrentIndex(index);
152}
153
154void SieveEditorTabWidget::slotLoadFinished(KSieveUi::SieveEditorHelpHtmlWidget *widget, bool success)
155{
156 const int index = indexOf(widget);
157 if (index != -1) {
158 setTabIcon(index, QIcon());
159 }
160 if (!success) {
161 setTabText(index, i18n("Error during load page about %1", widget->title()));
162 }
163}
164
165void SieveEditorTabWidget::slotProgressIndicatorPixmapChanged(KSieveUi::SieveEditorHelpHtmlWidget *widget, const QPixmap &pixmap)
166{
167 const int index = indexOf(widget);
168 if (index != -1) {
169 setTabIcon(index, QIcon(pixmap));
170 }
171}
172
173void SieveEditorTabWidget::slotTitleChanged(KSieveUi::SieveEditorHelpHtmlWidget *widget, const QString &title)
174{
175 const int index = indexOf(widget);
176 if (index != -1) {
177 const QString troncateTitle = KStringHandler::lsqueeze(title, 30);
178 setTabText(index, i18n("Help about: %1", troncateTitle));
179 setTabToolTip(index, title);
180 }
181}
182
183void SieveEditorTabWidget::tabRemoved(int index)
184{
185 if (count() <= 1) {
186 tabBar()->hide();
187 }
189}
190
191void SieveEditorTabWidget::tabInserted(int index)
192{
193 if (count() > 1) {
194 tabBar()->show();
195 }
198}
199
200#include "moc_sieveeditortabwidget.cpp"
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
KCOREADDONS_EXPORT QString lsqueeze(const QString &str, int maxlen=40)
void setEnabled(bool)
void setIcon(const QIcon &icon)
QIcon fromTheme(const QString &name)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
T qobject_cast(QObject *object)
CustomContextMenu
int tabAt(const QPoint &position) const const
QWidget * tabButton(int index, ButtonPosition position) const const
int addTab(QWidget *page, const QIcon &icon, const QString &label)
void setCurrentIndex(int index)
QWidget * currentWidget() const const
int indexOf(const QWidget *w) const const
void removeTab(int index)
void setTabIcon(int index, const QIcon &icon)
void setTabText(int index, const QString &label)
void setTabToolTip(int index, const QString &tip)
QTabBar * tabBar() const const
virtual void tabInserted(int index)
virtual void tabRemoved(int index)
QWidget * widget(int index) const const
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
void setEnabled(bool)
void hide()
QPoint mapFrom(const QWidget *parent, const QPoint &pos) const const
QPoint mapToGlobal(const QPoint &pos) const const
void show()
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:57:10 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.