Commit 700df52d authored by Waqar Ahmed's avatar Waqar Ahmed Committed by Christoph Cullmann
Browse files

Unify KateTabBar internal tab data

This is a step towards _some_ unification of how we handle
views/widgets.
parent 130fc518
Pipeline #252701 passed with stage
in 15 minutes and 8 seconds
......@@ -24,12 +24,6 @@
#include <KTextEditor/Document>
struct KateTabButtonData {
KTextEditor::Document *doc = nullptr;
};
Q_DECLARE_METATYPE(KateTabButtonData)
/**
* Creates a new tab bar with the given \a parent.
*/
......@@ -120,7 +114,7 @@ std::vector<int> KateTabBar::documentTabIndexes() const
std::vector<int> docs;
const int tabCount = count();
for (int i = 0; i < tabCount; ++i) {
if (tabData(i).value<KateTabButtonData>().doc) {
if (tabData(i).value<DocOrWidget>().doc()) {
docs.push_back(i);
}
}
......@@ -161,7 +155,8 @@ bool KateTabBar::containsTab(int index) const
QVariant KateTabBar::ensureValidTabData(int idx)
{
if (!tabData(idx).isValid()) {
setTabData(idx, QVariant::fromValue(KateTabButtonData{}));
DocOrWidget v(static_cast<KTextEditor::Document *>(nullptr));
setTabData(idx, QVariant::fromValue(v));
}
return tabData(idx);
}
......@@ -237,15 +232,10 @@ void KateTabBar::mouseMoveEvent(QMouseEvent *event)
}
QRect rect = tabRect(tab);
const auto tabData = this->tabData(tab);
QObject *tabObject = nullptr;
// TODO: unify KateTabButtonData with QWidget handling
if (auto doc = tabData.value<KateTabButtonData>().doc) {
tabObject = doc;
} else if (auto widget = tabData.value<QWidget *>()) {
tabObject = widget;
} else {
// shouldn't get here
const auto tabData = this->tabData(tab).value<DocOrWidget>();
auto *tabObject = tabData.qobject();
// We don't support moving widgets atm
if (!tabData.doc()) {
return;
}
......@@ -308,7 +298,8 @@ void KateTabBar::mouseMoveEvent(QMouseEvent *event)
int tabIdx = 0;
for (; tabIdx < count(); ++tabIdx) {
auto d = this->tabData(tabIdx);
if (d.value<KateTabButtonData>().doc == tabObject || d.value<QWidget *>() == tabObject) {
// We only expect doc, no dnd support for widgets
if (d.value<DocOrWidget>().doc() == tabObject) {
found = true;
break;
}
......@@ -337,10 +328,7 @@ void KateTabBar::contextMenuEvent(QContextMenuEvent *ev)
void KateTabBar::setTabDocument(int idx, KTextEditor::Document *doc)
{
QVariant data = ensureValidTabData(idx);
KateTabButtonData buttonData = data.value<KateTabButtonData>();
buttonData.doc = doc;
setTabData(idx, QVariant::fromValue(buttonData));
setTabData(idx, QVariant::fromValue(DocOrWidget(doc)));
// BUG: 441340 We need to escape the & because it is used for accelerators/shortcut mnemonic by default
QString tabName = doc->documentName();
tabName.replace(QLatin1Char('&'), QLatin1String("&&"));
......@@ -386,7 +374,7 @@ void KateTabBar::setCurrentDocument(KTextEditor::Document *doc)
KTextEditor::Document *docToReplace = nullptr;
for (int idx = 0; idx < count(); idx++) {
QVariant data = tabData(idx);
KTextEditor::Document *doc = data.value<KateTabButtonData>().doc;
KTextEditor::Document *doc = data.value<DocOrWidget>().doc();
if (!data.isValid() || !doc) {
continue;
}
......@@ -462,7 +450,7 @@ int KateTabBar::documentIdx(KTextEditor::Document *doc)
if (!data.isValid()) {
continue;
}
if (data.value<KateTabButtonData>().doc != doc) {
if (data.value<DocOrWidget>().doc() != doc) {
continue;
}
return idx;
......@@ -473,17 +461,17 @@ int KateTabBar::documentIdx(KTextEditor::Document *doc)
KTextEditor::Document *KateTabBar::tabDocument(int idx)
{
QVariant data = ensureValidTabData(idx);
KateTabButtonData buttonData = data.value<KateTabButtonData>();
DocOrWidget buttonData = data.value<DocOrWidget>();
KTextEditor::Document *doc = nullptr;
// The tab got activated before the correct finalixation,
// The tab got activated before the correct finalization,
// we need to plug the document before returning.
if (buttonData.doc == nullptr && m_beingAdded) {
if (buttonData.doc() == nullptr && m_beingAdded) {
setTabDocument(idx, m_beingAdded);
doc = m_beingAdded;
m_beingAdded = nullptr;
} else {
doc = buttonData.doc;
doc = buttonData.doc();
}
return doc;
......@@ -500,13 +488,12 @@ void KateTabBar::tabInserted(int idx)
QVector<KTextEditor::Document *> KateTabBar::documentList() const
{
QVector<KTextEditor::Document *> result;
result.reserve(count());
for (int idx = 0; idx < count(); idx++) {
QVariant data = tabData(idx);
if (!data.isValid()) {
continue;
if (data.isValid() && data.value<DocOrWidget>().doc()) {
result.append(data.value<DocOrWidget>().doc());
}
if (data.value<KateTabButtonData>().doc)
result.append(data.value<KateTabButtonData>().doc);
}
return result;
}
......@@ -515,6 +502,6 @@ void KateTabBar::setCurrentWidget(QWidget *widget)
{
int idx = insertTab(-1, widget->windowTitle());
setTabIcon(idx, widget->windowIcon());
setTabData(idx, QVariant::fromValue(widget));
setTabData(idx, QVariant::fromValue(DocOrWidget(widget)));
setCurrentIndex(idx);
}
......@@ -444,9 +444,10 @@ void KateViewSpace::changeView(int idx)
m_viewManager->setActiveSpace(this);
}
KTextEditor::Document *doc = m_tabBar->tabDocument(idx);
const auto docOrWidget = m_tabBar->tabData(idx).value<DocOrWidget>();
KTextEditor::Document *doc = docOrWidget.doc();
if (!doc) {
auto w = m_tabBar->tabData(idx).value<QWidget *>();
auto w = docOrWidget.widget();
if (!w) {
// can happen during widget creation if no view is there initially
return;
......@@ -711,9 +712,10 @@ void KateViewSpace::updateDocumentUrl(KTextEditor::Document *doc)
void KateViewSpace::closeTabRequest(int idx)
{
auto *doc = m_tabBar->tabDocument(idx);
const auto docOrWidget = m_tabBar->tabData(idx).value<DocOrWidget>();
auto *doc = docOrWidget.doc();
if (!doc) {
auto widget = m_tabBar->tabData(idx).value<QWidget *>();
auto widget = docOrWidget.widget();
if (!widget) {
Q_ASSERT(false);
return;
......@@ -813,7 +815,7 @@ QWidgetList KateViewSpace::widgets() const
{
QWidgetList widgets;
for (int i = 0; i < m_tabBar->count(); ++i) {
auto widget = m_tabBar->tabData(i).value<QWidget *>();
auto widget = m_tabBar->tabData(i).value<DocOrWidget>().widget();
if (widget) {
widgets << widget;
}
......@@ -828,7 +830,7 @@ bool KateViewSpace::closeTabWithWidget(QWidget *widget)
}
for (int i = 0; i < m_tabBar->count(); ++i) {
if (m_tabBar->tabData(i).value<QWidget *>() == widget) {
if (m_tabBar->tabData(i).value<DocOrWidget>().widget() == widget) {
closeTabRequest(i);
return true;
}
......@@ -844,7 +846,7 @@ bool KateViewSpace::activateWidget(QWidget *widget)
stack->setCurrentWidget(widget);
for (int i = 0; i < m_tabBar->count(); ++i) {
if (m_tabBar->tabData(i).value<QWidget *>() == widget) {
if (m_tabBar->tabData(i).value<DocOrWidget>().widget() == widget) {
m_registeredDocuments.removeOne(widget);
m_registeredDocuments.append(widget);
// "activation signal, ViewManager::viewChanged" will be emitted
......
......@@ -24,6 +24,38 @@ class QStackedWidget;
class QToolButton;
class LocationHistoryTest;
// Just a helper class which we use internally to manage widgets/docs
class DocOrWidget : public std::variant<KTextEditor::Document *, QWidget *>
{
public:
using variant::variant;
auto *doc() const
{
return std::holds_alternative<KTextEditor::Document *>(*this) ? std::get<KTextEditor::Document *>(*this) : nullptr;
}
auto *widget() const
{
return std::holds_alternative<QWidget *>(*this) ? std::get<QWidget *>(*this) : nullptr;
}
QObject *qobject() const
{
return doc() ? static_cast<QObject *>(doc()) : static_cast<QObject *>(widget());
}
bool operator==(KTextEditor::Document *doc) const
{
return this->doc() == doc;
}
bool operator==(QWidget *w) const
{
return this->widget() == w;
}
};
Q_DECLARE_METATYPE(DocOrWidget)
class KATE_PRIVATE_EXPORT KateViewSpace : public QWidget
{
Q_OBJECT
......@@ -298,28 +330,6 @@ private:
std::vector<Location> m_locations;
size_t currentLocation = 0;
struct DocOrWidget : public std::variant<KTextEditor::Document *, QWidget *> {
using variant::variant;
auto *doc() const
{
return std::holds_alternative<KTextEditor::Document *>(*this) ? std::get<KTextEditor::Document *>(*this) : nullptr;
}
auto widget() const
{
return std::holds_alternative<QWidget *>(*this) ? std::get<QWidget *>(*this) : nullptr;
}
bool operator==(KTextEditor::Document *doc) const
{
return this->doc() == doc;
}
bool operator==(QWidget *w) const
{
return this->widget() == w;
}
};
/**
* all documents this view space is aware of
* depending on the limit of tabs, not all will have a corresponding
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment