diff --git a/libs/ui/widgets/kis_workspace_chooser.cpp b/libs/ui/widgets/kis_workspace_chooser.cpp index 3f350712ba..6c4e60bf96 100644 --- a/libs/ui/widgets/kis_workspace_chooser.cpp +++ b/libs/ui/widgets/kis_workspace_chooser.cpp @@ -1,204 +1,229 @@ /* This file is part of the KDE project * Copyright (C) 2011 Sven Langkamp * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public License * along with this library; see the file COPYING.LIB. If not, write to * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301, USA. */ #include "kis_workspace_chooser.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include -#include +#include #include "kis_workspace_resource.h" #include "KisViewManager.h" #include "kis_canvas_resource_provider.h" #include "KisMainWindow.h" #include "KisPart.h" #include "KisWindowLayoutManager.h" #include "dialogs/KisNewWindowLayoutDialog.h" #include "kis_config.h" +#include class KisWorkspaceDelegate : public QAbstractItemDelegate { public: KisWorkspaceDelegate(QObject * parent = 0) : QAbstractItemDelegate(parent) {} ~KisWorkspaceDelegate() override {} /// reimplemented void paint(QPainter *, const QStyleOptionViewItem &, const QModelIndex &) const override; /// reimplemented QSize sizeHint(const QStyleOptionViewItem & option, const QModelIndex &) const override { return option.decorationSize; } }; void KisWorkspaceDelegate::paint(QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index) const { if (!index.isValid()) return; QPalette::ColorGroup cg = (option.state & QStyle::State_Enabled) ? QPalette::Active : QPalette::Disabled; QPalette::ColorRole cr = (option.state & QStyle::State_Selected) ? QPalette::HighlightedText : QPalette::Text; painter->setPen(option.palette.color(cg, cr)); if (option.state & QStyle::State_Selected) { painter->fillRect(option.rect, option.palette.highlight()); } else { painter->fillRect(option.rect, option.palette.base()); } QString name = index.data(Qt::UserRole + KisResourceModel::Name).toString(); painter->drawText(option.rect.x() + 5, option.rect.y() + painter->fontMetrics().ascent() + 5, name); } KisWorkspaceChooser::KisWorkspaceChooser(KisViewManager * view, QWidget* parent): QWidget(parent), m_view(view) { m_layout = new QGridLayout(this); m_workspaceWidgets = createChooserWidgets(ResourceType::Workspaces, i18n("Workspaces")); m_windowLayoutWidgets = createChooserWidgets(ResourceType::WindowLayouts, i18n("Window layouts")); connect(m_workspaceWidgets.itemChooser, SIGNAL(resourceSelected(KoResourceSP )), this, SLOT(workspaceSelected(KoResourceSP ))); connect(m_workspaceWidgets.saveButton, SIGNAL(clicked(bool)), this, SLOT(slotSaveWorkspace())); + connect(m_workspaceWidgets.nameEdit, SIGNAL(textEdited(const QString&)), this, SLOT(slotUpdateWorkspaceSaveButton())); connect(m_windowLayoutWidgets.itemChooser, SIGNAL(resourceSelected(KoResourceSP )), this, SLOT(windowLayoutSelected(KoResourceSP ))); connect(m_windowLayoutWidgets.saveButton, SIGNAL(clicked(bool)), this, SLOT(slotSaveWindowLayout())); + connect(m_windowLayoutWidgets.nameEdit, SIGNAL(textEdited(const QString&)), this, SLOT(slotUpdateWindowLayoutSaveButton())); } KisWorkspaceChooser::ChooserWidgets KisWorkspaceChooser::createChooserWidgets(const QString &resourceType, const QString &title) { ChooserWidgets widgets; QLabel *titleLabel = new QLabel(this); QFont titleFont; titleFont.setBold(true); titleLabel->setFont(titleFont); titleLabel->setText(title); + m_workspaceSaveLocation = KisResourceServerProvider::instance()->workspaceServer()->saveLocation(); + m_windowLayoutSaveLocation = KisResourceServerProvider::instance()->windowLayoutServer()->saveLocation(); + widgets.itemChooser = new KisResourceItemChooser(resourceType, false, this); widgets.itemChooser->setItemDelegate(new KisWorkspaceDelegate(this)); widgets.itemChooser->setFixedSize(250, 250); widgets.itemChooser->setRowHeight(30); widgets.itemChooser->setColumnCount(1); widgets.itemChooser->showTaggingBar(false); widgets.saveButton = new QPushButton(i18n("Save")); widgets.nameEdit = new QLineEdit(this); widgets.nameEdit->setPlaceholderText(i18n("Insert name")); widgets.nameEdit->setClearButtonEnabled(true); int firstRow = m_layout->rowCount(); m_layout->addWidget(titleLabel, firstRow, 0, 1, 2); m_layout->addWidget(widgets.itemChooser, firstRow + 1, 0, 1, 2); m_layout->addWidget(widgets.nameEdit, firstRow + 2, 0, 1, 1); m_layout->addWidget(widgets.saveButton, firstRow + 2, 1, 1, 1); return widgets; } KisWorkspaceChooser::~KisWorkspaceChooser() { } void KisWorkspaceChooser::slotSaveWorkspace() { if (!m_view->qtMainWindow()) { return; } KisWorkspaceResourceSP workspace(new KisWorkspaceResource(QString())); workspace->setDockerState(m_view->qtMainWindow()->saveState()); m_view->canvasResourceProvider()->notifySavingWorkspace(workspace); workspace->setValid(true); QString name = m_workspaceWidgets.nameEdit->text(); - if (name.isEmpty()) { - name = i18n("Workspace"); - } - workspace->setName(name); workspace->setFilename(name.split(" ").join("_")+workspace->defaultFileExtension()); - KisResourceModelProvider::resourceModel(ResourceType::Workspaces)->addResource(workspace); } +void KisWorkspaceChooser::slotUpdateWorkspaceSaveButton() +{ + if (QFileInfo(m_workspaceSaveLocation + m_workspaceWidgets.nameEdit->text().split(" ").join("_") + ".kws").exists()) { + m_workspaceWidgets.saveButton->setIcon(KisIconUtils::loadIcon("warning")); + m_workspaceWidgets.saveButton->setToolTip(i18n("File name already in use. Saving will overwrite the original Workspace.")); + //m_workspaceWidgets.saveButton->setText(i18n("Overwrite")); + } else { + m_workspaceWidgets.saveButton->setIcon(QIcon()); + m_workspaceWidgets.saveButton->setToolTip(i18n("Save current workspace.")); + //m_workspaceWidgets.saveButton->setText(i18n("Save")); + } +} + void KisWorkspaceChooser::workspaceSelected(KoResourceSP resource) { if (!m_view->qtMainWindow()) { return; } KisConfig cfg(false); cfg.writeEntry("CurrentWorkspace", resource->name()); KisWorkspaceResourceSP workspace = resource.staticCast(); KisMainWindow *mainWindow = qobject_cast(m_view->qtMainWindow()); mainWindow->restoreWorkspace(workspace->resourceId()); } void KisWorkspaceChooser::slotSaveWindowLayout() { KisMainWindow *thisWindow = qobject_cast(m_view->qtMainWindow()); if (!thisWindow) return; KisNewWindowLayoutDialog dlg; dlg.setName(m_windowLayoutWidgets.nameEdit->text()); dlg.exec(); if (dlg.result() != QDialog::Accepted) return; QString name = dlg.name(); bool showImageInAllWindows = dlg.showImageInAllWindows(); bool primaryWorkspaceFollowsFocus = dlg.primaryWorkspaceFollowsFocus(); KisWindowLayoutResourceSP layout = KisWindowLayoutResource::fromCurrentWindows(name, KisPart::instance()->mainWindows(), showImageInAllWindows, primaryWorkspaceFollowsFocus, thisWindow); layout->setValid(true); KisWindowLayoutManager::instance()->setShowImageInAllWindowsEnabled(showImageInAllWindows); KisWindowLayoutManager::instance()->setPrimaryWorkspaceFollowsFocus(primaryWorkspaceFollowsFocus, thisWindow->id()); if (name.isEmpty()) { name = i18n("Window Layout"); } layout->setName(name); layout->setFilename(name.split(" ").join("_") + layout->defaultFileExtension()); KisResourceModelProvider::resourceModel(ResourceType::WindowLayouts)->addResource(layout); } +void KisWorkspaceChooser::slotUpdateWindowLayoutSaveButton() +{ + if (QFileInfo(m_windowLayoutSaveLocation + m_windowLayoutWidgets.nameEdit->text().split(" ").join("_") + ".kwl").exists()) { + m_windowLayoutWidgets.saveButton->setIcon(KisIconUtils::loadIcon("warning")); + m_workspaceWidgets.saveButton->setToolTip(i18n("File name already in use. Saving will overwrite the original window layout.")); + } else { + m_windowLayoutWidgets.saveButton->setIcon(QIcon()); + m_workspaceWidgets.saveButton->setToolTip(i18n("Save current window layout.")); + } +} + void KisWorkspaceChooser::windowLayoutSelected(KoResourceSP resource) { KisWindowLayoutResourceSP layout = resource.staticCast(); layout->applyLayout(); } diff --git a/libs/ui/widgets/kis_workspace_chooser.h b/libs/ui/widgets/kis_workspace_chooser.h index d1c1fa981d..71389ccf42 100644 --- a/libs/ui/widgets/kis_workspace_chooser.h +++ b/libs/ui/widgets/kis_workspace_chooser.h @@ -1,64 +1,69 @@ /* This file is part of the KDE project * Copyright (C) 2011 Sven Langkamp * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public License * along with this library; see the file COPYING.LIB. If not, write to * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301, USA. */ #ifndef KIS_WORKSPACE_CHOOSER_H #define KIS_WORKSPACE_CHOOSER_H #include #include class QLineEdit; class QPushButton; class QGridLayout; class KisResourceItemChooser; class KisViewManager; class KisWorkspaceChooser : public QWidget { Q_OBJECT public: KisWorkspaceChooser(KisViewManager * view, QWidget* parent = 0); ~KisWorkspaceChooser() override; private Q_SLOTS: void slotSaveWorkspace(); + void slotUpdateWorkspaceSaveButton(); void workspaceSelected( KoResourceSP resource ); void slotSaveWindowLayout(); + void slotUpdateWindowLayoutSaveButton(); void windowLayoutSelected( KoResourceSP resource ); private: struct ChooserWidgets { KisResourceItemChooser *itemChooser; QLineEdit *nameEdit; QPushButton *saveButton; }; KisViewManager *m_view; QGridLayout* m_layout; ChooserWidgets m_workspaceWidgets; ChooserWidgets m_windowLayoutWidgets; + QString m_workspaceSaveLocation; + QString m_windowLayoutSaveLocation; + ChooserWidgets createChooserWidgets(const QString &resourceType, const QString &title); }; #endif // KIS_WORKSPACE_CHOOSER_H