Krita Source Code Documentation
Loading...
Searching...
No Matches
kis_workspace_chooser.cpp
Go to the documentation of this file.
1/* This file is part of the KDE project
2 * SPDX-FileCopyrightText: 2011 Sven Langkamp <sven.langkamp@gmail.com>
3 *
4 * SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6
8
9#include <QVBoxLayout>
10#include <QStyledItemDelegate>
11#include <QPainter>
12#include <QPushButton>
13#include <QAction>
14#include <QGridLayout>
15#include <QLineEdit>
16#include <QLabel>
17#include <KisKineticScroller.h>
18
19#include <klocalizedstring.h>
20
23#include <KoResource.h>
24#include <KisResourceModel.h>
27#include <KisMainWindow.h>
29
31#include "KisViewManager.h"
33#include "KisMainWindow.h"
34#include "KisPart.h"
37#include "kis_config.h"
38#include <kis_icon.h>
40
41
42class KisWorkspaceDelegate : public QStyledItemDelegate
43{
44public:
45 static const int heightHint = 30;
46
47public:
48 KisWorkspaceDelegate(QObject * parent = 0) : QStyledItemDelegate(parent) {}
50
51
52 void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const override
53 {
54 if (!index.isValid())
55 return;
56
57 QPalette::ColorGroup cg = (option.state & QStyle::State_Enabled) ? QPalette::Active : QPalette::Disabled;
58 QPalette::ColorRole cr = (option.state & QStyle::State_Selected) ? QPalette::HighlightedText : QPalette::Text;
59 painter->setPen(option.palette.color(cg, cr));
60
61 if (option.state & QStyle::State_Selected) {
62 painter->fillRect(option.rect, option.palette.highlight());
63 }
64 else {
65 painter->fillRect(option.rect, option.palette.base());
66 }
67
68 int verticalPadding = (option.rect.height() - painter->fontMetrics().ascent()) / 2;
69 const int correction = -3;
70
71 QString name = index.data(Qt::UserRole + KisAbstractResourceModel::Name).toString();
72 painter->drawText(
73 option.rect.x() + 5,
74 option.rect.y() + verticalPadding + painter->fontMetrics().ascent() + correction,
75 name
76 );
77 }
78
79 QSize sizeHint(const QStyleOptionViewItem&, const QModelIndex &) const override
80 {
81 return QSize(-1, heightHint);
82 }
83};
84
85KisWorkspaceChooser::KisWorkspaceChooser(KisViewManager* view, QWidget* parent): QWidget(parent), m_view(view)
86{
87 m_layout = new QGridLayout(this);
88
89 // Workspaces
91
92 KisConfig cfg(true);
93 QString workspaceName = cfg.readEntry<QString>("CurrentWorkspace");
95
96 connect(m_workspaceWidgets.itemChooser, SIGNAL(resourceSelected(KoResourceSP )),
97 this, SLOT(workspaceSelected(KoResourceSP )));
98 connect(m_workspaceWidgets.saveButton, SIGNAL(clicked(bool)), this, SLOT(slotSaveWorkspace()));
99 connect(m_workspaceWidgets.nameEdit, SIGNAL(textEdited(const QString&)), this, SLOT(slotUpdateWorkspaceSaveButton()));
100
102
103 // Window layouts
105
106 connect(m_windowLayoutWidgets.itemChooser, SIGNAL(resourceSelected(KoResourceSP )),
107 this, SLOT(windowLayoutSelected(KoResourceSP )));
108 connect(m_windowLayoutWidgets.saveButton, SIGNAL(clicked(bool)), this, SLOT(slotSaveWindowLayout()));
109 connect(m_windowLayoutWidgets.nameEdit, SIGNAL(textEdited(const QString&)), this, SLOT(slotUpdateWindowLayoutSaveButton()));
110
112}
113
115{
117
118 QLabel *titleLabel = new QLabel(this);
119 QFont titleFont;
120 titleFont.setBold(true);
121 titleLabel->setFont(titleFont);
122 titleLabel->setText(title);
123
124 r.itemChooser = new KisResourceItemChooser(resourceType, false, this);
125 r.itemChooser->setItemDelegate(new KisWorkspaceDelegate(this));
126 r.itemChooser->setListViewMode(ListViewMode::Detail);
127 r.itemChooser->setRowHeight(KisWorkspaceDelegate::heightHint);
128 r.itemChooser->showTaggingBar(false);
129
130 r.nameEdit = new QLineEdit(this);
131 r.nameEdit->setPlaceholderText(i18n("Insert name"));
132 r.nameEdit->setClearButtonEnabled(true);
133
134 r.saveButton = new QPushButton(i18n("Save"));
135
136 int firstRow = m_layout->rowCount();
137 m_layout->addWidget(titleLabel, firstRow, 0, 1, 2);
138 m_layout->addWidget(r.itemChooser, firstRow + 1, 0, 1, 2);
139 m_layout->addWidget(r.nameEdit, firstRow + 2, 0, 1, 1);
140 m_layout->addWidget(r.saveButton, firstRow + 2, 1, 1, 1);
141
142 return r;
143}
144
149
151{
152 if (!m_view->qtMainWindow()) {
153 return;
154 }
155
156 KisWorkspaceResourceSP workspace;
157 QString name = m_workspaceWidgets.nameEdit->text();
158 if (name.isEmpty()) {
159 return;
160 }
161
163 QVector<KoResourceSP> resources = resourceModel->resourcesForName(name);
164
165 if (!resources.isEmpty()) {
166 workspace = resources.first().dynamicCast<KisWorkspaceResource>();
167 }
168
169 if (workspace.isNull()) {
170 workspace.reset(new KisWorkspaceResource(name));
171 workspace->setName(name);
172 workspace->setDockerState(m_view->qtMainWindow()->saveState());
173 workspace->setImage(m_view->mainWindow()->layoutThumbnail());
174 workspace->setValid(true);
175 workspace->setFilename(name.replace(" ", "_") + workspace->defaultFileExtension());
178 }
179 else {
180 workspace->setDockerState(m_view->qtMainWindow()->saveState());
181 workspace->setImage(m_view->mainWindow()->layoutThumbnail());
184 }
185
188}
189
191{
192 if (m_workspaceWidgets.nameEdit->text().isEmpty()) {
193 m_workspaceWidgets.saveButton->setEnabled(false);
194 return;
195 }
196 m_workspaceWidgets.saveButton->setEnabled(true);
197
200 KoResourceSP res = resources.count() > 0 ? resources.first() : nullptr;
201 if (res && res->active()) {
203 m_workspaceWidgets.saveButton->setToolTip(i18n("File name already in use. Saving will overwrite the original Workspace."));
204 m_workspaceWidgets.saveButton->setText(i18n("Overwrite"));
205 }
206 else {
207 m_workspaceWidgets.saveButton->setIcon(QIcon());
208 m_workspaceWidgets.saveButton->setToolTip(i18n("Save current workspace."));
209 m_workspaceWidgets.saveButton->setText(i18n("Save"));
210 }
211}
212
214{
215 if (!m_view->qtMainWindow()) {
216 return;
217 }
218
219 KisConfig cfg(false);
220 cfg.writeEntry("CurrentWorkspace", resource->name());
221 KisWorkspaceResourceSP workspace = resource.dynamicCast<KisWorkspaceResource>();
222 KisMainWindow *mainWindow = qobject_cast<KisMainWindow*>(m_view->qtMainWindow());
223 mainWindow->restoreWorkspace(workspace);
224}
225
227{
228 KisMainWindow *thisWindow = qobject_cast<KisMainWindow*>(m_view->qtMainWindow());
229 if (!thisWindow) return;
230
231 if (m_windowLayoutWidgets.nameEdit->text().isEmpty()) {
232 return;
233 }
234
237 dlg.exec();
238
239 if (dlg.result() != QDialog::Accepted) return;
240
241 QString name = dlg.name();
242 bool showImageInAllWindows = dlg.showImageInAllWindows();
243 bool primaryWorkspaceFollowsFocus = dlg.primaryWorkspaceFollowsFocus();
244
245 KisWindowLayoutResourceSP layout = KisWindowLayoutResource::fromCurrentWindows(name, KisPart::instance()->mainWindows(), showImageInAllWindows, primaryWorkspaceFollowsFocus, thisWindow);
246 layout->setValid(true);
247
249 KisWindowLayoutManager::instance()->setPrimaryWorkspaceFollowsFocus(primaryWorkspaceFollowsFocus, thisWindow->id());
250
251 if (name.isEmpty()) {
252 name = i18n("Window Layout");
253 }
254
255 layout->setName(name);
256 layout->setFilename(name.split(" ").join("_") + layout->defaultFileExtension());
258
260}
261
263{
264 if (m_windowLayoutWidgets.nameEdit->text().isEmpty()) {
265 m_windowLayoutWidgets.saveButton->setEnabled(false);
266 return;
267 }
268 m_windowLayoutWidgets.saveButton->setEnabled(true);
269
272 if (resources.isEmpty()) return;
273
274 KoResourceSP res = resources.first();
275
276 if (res && res->active()) {
278 m_windowLayoutWidgets.saveButton->setToolTip(i18n("File name already in use. Saving will overwrite the original window layout."));
279 } else {
280 m_windowLayoutWidgets.saveButton->setIcon(QIcon());
281 m_windowLayoutWidgets.saveButton->setToolTip(i18n("Save current window layout."));
282 }
283}
284
286{
287 KisWindowLayoutResourceSP layout = resource.dynamicCast<KisWindowLayoutResource>();
288 layout->applyLayout();
289}
290
291// WORKAROUND: setting the row height of the resource chooser gets overridden somehow
292// so set it again here
connect(this, SIGNAL(optionsChanged()), this, SLOT(saveOptions()))
The KisAllresourcesModel class provides access to the cache database for a particular resource type....
QVector< KoResourceSP > resourcesForName(const QString &name) const
void notifySavingWorkspace(KisWorkspaceResourceSP workspace)
Notify that the workspace is saved and settings should be saved to it.
void writeEntry(const QString &name, const T &value)
Definition kis_config.h:779
T readEntry(const QString &name, const T &defaultValue=T())
Definition kis_config.h:789
Main window for Krita.
QImage layoutThumbnail()
layoutThumbnail
void setName(const QString &name)
static KisPart * instance()
Definition KisPart.cpp:131
void setCurrentResource(KoResourceSP resource)
Sets the item representing the resource as selected.
void setRowHeight(int rowHeight)
Sets the height of the view rows.
static KisAllResourcesModel * resourceModel(const QString &resourceType)
static bool addResourceWithUserInput(QWidget *widgetParent, KoResourceSP resource, QString storageLocation="")
static bool updateResourceWithUserInput(QWidget *widgetParent, KoResourceSP resource)
KisMainWindow * mainWindow() const
QMainWindow * qtMainWindow() const
KisCanvasResourceProvider * canvasResourceProvider()
void setShowImageInAllWindowsEnabled(bool showInAll)
void setPrimaryWorkspaceFollowsFocus(bool enabled, QUuid primaryWindow)
static KisWindowLayoutManager * instance()
static KisWindowLayoutResourceSP fromCurrentWindows(const QString &filename, const QList< QPointer< KisMainWindow > > &mainWindows, bool showImageInAllWindows, bool primaryWorkspaceFollowsFocus, KisMainWindow *primaryWindow)
ChooserWidgets m_workspaceWidgets
ChooserWidgets m_windowLayoutWidgets
ChooserWidgets createChooserWidgets(const QString &resourceType, const QString &title)
void showEvent(QShowEvent *) override
KisWorkspaceChooser(KisViewManager *view, QWidget *parent=0)
void workspaceSelected(KoResourceSP resource)
void windowLayoutSelected(KoResourceSP resource)
KisWorkspaceDelegate(QObject *parent=0)
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override
QSize sizeHint(const QStyleOptionViewItem &, const QModelIndex &) const override
Resource for storing of workspaces.
QIcon loadIcon(const QString &name)
const QString Workspaces
const QString WindowLayouts