1 /*
2 SPDX-FileCopyrightText: David Nolden <david.nolden.kdevelop@art-master.de>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
7 #include "workingsettoolbutton.h"
8
9 #include <QContextMenuEvent>
10
11 #include <sublime/mainwindow.h>
12 #include <sublime/area.h>
13
14 #include "core.h"
15 #include "mainwindow.h"
16 #include "workingset.h"
17 #include "workingsetcontroller.h"
18 #include "workingsethelpers.h"
19 #include "documentcontroller.h"
20
21 #include <interfaces/idocumentcontroller.h>
22 #include <util/activetooltip.h>
23
24 using namespace KDevelop;
25
WorkingSetToolButton(QWidget * parent,WorkingSet * set)26 WorkingSetToolButton::WorkingSetToolButton(QWidget* parent, WorkingSet* set)
27 : QToolButton(parent), m_set(set), m_toolTipEnabled(true)
28 {
29 setFocusPolicy(Qt::NoFocus);
30 setWorkingSet(set);
31 setAutoRaise(true);
32
33 connect(this, &WorkingSetToolButton::clicked, this, &WorkingSetToolButton::buttonTriggered);
34 }
35
workingSet() const36 WorkingSet* WorkingSetToolButton::workingSet() const
37 {
38 return m_set;
39 }
40
setWorkingSet(WorkingSet * set)41 void WorkingSetToolButton::setWorkingSet(WorkingSet* set)
42 {
43 m_set = set;
44
45 setIcon(set ? set->icon() : QIcon());
46 }
47
contextMenuEvent(QContextMenuEvent * ev)48 void WorkingSetToolButton::contextMenuEvent(QContextMenuEvent* ev)
49 {
50 showTooltip(ev->globalPos());
51
52 ev->accept();
53 }
54
intersectSet()55 void WorkingSetToolButton::intersectSet()
56 {
57 Q_ASSERT(m_set);
58
59 m_set->setPersistent(true);
60
61 filterViews(Core::self()->workingSetControllerInternal()->workingSet(mainWindow()->area()->workingSet())->fileSet() & m_set->fileSet());
62 }
63
subtractSet()64 void WorkingSetToolButton::subtractSet()
65 {
66 Q_ASSERT(m_set);
67
68 m_set->setPersistent(true);
69
70 filterViews(Core::self()->workingSetControllerInternal()->workingSet(mainWindow()->area()->workingSet())->fileSet() - m_set->fileSet());
71 }
72
mergeSet()73 void WorkingSetToolButton::mergeSet()
74 {
75 Q_ASSERT(m_set);
76
77 const QSet<QString> loadFiles = m_set->fileSet() - Core::self()->workingSetControllerInternal()->workingSet(mainWindow()->area()->workingSet())->fileSet();
78 for (const QString& file : loadFiles) {
79 Core::self()->documentController()->openDocument(QUrl::fromUserInput(file));
80 }
81 }
82
duplicateSet()83 void WorkingSetToolButton::duplicateSet()
84 {
85 Q_ASSERT(m_set);
86
87 if(!Core::self()->documentControllerInternal()->saveAllDocumentsForWindow(mainWindow(), KDevelop::IDocument::Default, true))
88 return;
89 WorkingSet* set = Core::self()->workingSetControllerInternal()->newWorkingSet(QStringLiteral("clone"));
90 set->setPersistent(true);
91 set->saveFromArea(mainWindow()->area());
92 mainWindow()->area()->setWorkingSet(set->id());
93 }
94
loadSet()95 void WorkingSetToolButton::loadSet()
96 {
97 Q_ASSERT(m_set);
98
99 m_set->setPersistent(true);
100
101 if(!Core::self()->documentControllerInternal()->saveAllDocumentsForWindow(mainWindow(), KDevelop::IDocument::Default, true))
102 return;
103 mainWindow()->area()->setWorkingSet(QString(m_set->id()));
104 }
105
closeSet()106 void WorkingSetToolButton::closeSet()
107 {
108 Q_ASSERT(m_set);
109
110 m_set->setPersistent(true);
111 m_set->saveFromArea(mainWindow()->area());
112
113 if(!Core::self()->documentControllerInternal()->saveAllDocumentsForWindow(mainWindow(), KDevelop::IDocument::Default, true))
114 return;
115 mainWindow()->area()->setWorkingSet(QString());
116 }
117
event(QEvent * e)118 bool WorkingSetToolButton::event(QEvent* e)
119 {
120 if(m_toolTipEnabled && e->type() == QEvent::ToolTip) {
121 auto* helpEvent = static_cast<QHelpEvent*>(e);
122 showTooltip(helpEvent->globalPos());
123 e->accept();
124 return true;
125 }
126
127 return QToolButton::event(e);
128 }
129
showTooltip(const QPoint & globalPos)130 void WorkingSetToolButton::showTooltip(const QPoint& globalPos)
131 {
132 Q_ASSERT(m_set);
133 static WorkingSetToolButton* oldTooltipButton;
134
135 WorkingSetController* controller = Core::self()->workingSetControllerInternal();
136
137 if(controller->tooltip() && oldTooltipButton == this)
138 return;
139
140 oldTooltipButton = this;
141
142 controller->showToolTip(m_set, globalPos + QPoint(10, 20));
143
144 QRect extended(parentWidget()->mapToGlobal(geometry().topLeft()),
145 parentWidget()->mapToGlobal(geometry().bottomRight()));
146 controller->tooltip()->setHandleRect(extended);
147 }
148
buttonTriggered()149 void WorkingSetToolButton::buttonTriggered()
150 {
151 Q_ASSERT(m_set);
152
153 if(mainWindow()->area()->workingSet() == m_set->id()) {
154 showTooltip(QCursor::pos());
155 }else{
156 //Only close the working-set if the file was saved before
157 if(!Core::self()->documentControllerInternal()->saveAllDocumentsForWindow(mainWindow(), KDevelop::IDocument::Default, true))
158 return;
159 m_set->setPersistent(true);
160 mainWindow()->area()->setWorkingSet(m_set->id());
161 }
162 }
163
164