Krita Source Code Documentation
Loading...
Searching...
No Matches
kis_clipboard_brush_widget.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2005 Bart Coppens <kde@bartcoppens.be>
3 * SPDX-FileCopyrightText: 2010 Lukáš Tvrdý <lukast.dev@gmail.com>
4 * SPDX-FileCopyrightText: 2013 Somsubhra Bairi <somsubhra.bairi@gmail.com>
5 *
6 * SPDX-License-Identifier: GPL-2.0-or-later
7 */
8
10
11#include <QPixmap>
12#include <QPushButton>
13#include <QShowEvent>
14#include <QDialogButtonBox>
15#include <QUuid>
16
17#include <KoResourcePaths.h>
18
19#include <kis_debug.h>
20#include "kis_image.h"
21#include "kis_clipboard.h"
22#include "kis_paint_device.h"
23#include "kis_gbr_brush.h"
25#include "kis_icon.h"
27
28#include <kstandardguiitem.h>
29
30KisClipboardBrushWidget::KisClipboardBrushWidget(QWidget *parent, const QString &caption, KisImageWSP /*image*/)
31 : KisWdgClipboardBrush(parent)
32{
33 setWindowTitle(caption);
34 preview->setScaledContents(false);
35 preview->setFixedSize(preview->size());
36 preview->setStyleSheet("border: 2px solid #222; border-radius: 4px; padding: 5px; font: normal 10px;");
37
38
40
41 m_brush = 0;
42
44
45 connect(m_clipboard, SIGNAL(clipChanged()), this, SLOT(slotClipboardContentChanged()));
46 connect(colorAsmask, SIGNAL(toggled(bool)), this, SLOT(slotUpdateUseColorAsMask(bool)));
47 connect(preserveAlpha, SIGNAL(toggled(bool)), this, SLOT(slotCreateBrush()));
48 connect(buttonBox, SIGNAL(accepted()), this, SLOT(slotAddPredefined()));
49 connect(nameEdit, SIGNAL(textEdited(const QString&)), this, SLOT(slotUpdateSaveButton()));
50
51 spacingWidget->setSpacing(true, 1.0);
52 connect(spacingWidget, SIGNAL(sigSpacingChanged()), SLOT(slotSpacingChanged()));
53
54 KGuiItem::assign(buttonBox->button(QDialogButtonBox::Save), KStandardGuiItem::save());
55 KGuiItem::assign(buttonBox->button(QDialogButtonBox::Cancel), KStandardGuiItem::cancel());
56}
57
61
63{
65 if (m_brush) {
66 colorAsmask->setChecked(true); // initializing this has to happen here since we need a valid brush for it to work
67 preserveAlpha->setEnabled(true);
68 preserveAlpha->setChecked(false);
69 }
70}
71
73{
74 // do nothing if it's hidden otherwise it can break the active brush is something is copied
75 if (m_clipboard->hasClip() && !isHidden()) {
76
77 pd = m_clipboard->clip(QRect(0, 0, 0, 0), false); //Weird! Don't know how this works!
78 if (pd) {
79 QRect rc = pd->exactBounds();
80
81 m_brush = KisBrushSP(new KisGbrBrush(pd, rc.x(), rc.y(), rc.width(), rc.height()));
82
83 m_brush->setSpacing(spacingWidget->spacing());
84 m_brush->setAutoSpacing(spacingWidget->autoSpacingActive(), spacingWidget->autoSpacingCoeff());
87 m_brush->setValid(true);
88
89 if (colorAsmask->isChecked()) {
90 static_cast<KisGbrBrush*>(m_brush.data())->makeMaskImage(preserveAlpha->isChecked());
91 static_cast<KisGbrBrush*>(m_brush.data())->setBrushApplication(preserveAlpha->isChecked() ? LIGHTNESSMAP : ALPHAMASK);
92 } else {
93 static_cast<KisGbrBrush*>(m_brush.data())->setBrushApplication(IMAGESTAMP);
94 }
95
96 int w = preview->size().width()-10;
97 preview->setPixmap(QPixmap::fromImage(m_brush->image().scaled(w, w, Qt::KeepAspectRatio)));
98 }
99 } else {
100 preview->setText(i18n("Nothing copied\n to Clipboard"));
101 }
102
103 if (!m_brush) {
104 buttonBox->button(QDialogButtonBox::Save)->setEnabled(false);
105 } else {
106 buttonBox->button(QDialogButtonBox::Save)->setEnabled(true);
107 }
108}
109
111{
112 if (m_brush) {
113 m_brush->setSpacing(spacingWidget->spacing());
114 m_brush->setAutoSpacing(spacingWidget->autoSpacingActive(), spacingWidget->autoSpacingCoeff());
115 }
116}
117
122
124{
125 preserveAlpha->setEnabled(useColorAsMask);
127}
128
130{
131 if(!m_brush) return;
132
133 QString extension = ".gbr";
134 QString name = nameEdit->text();
135
136 if (m_rServer) {
137 KisGbrBrushSP resource = m_brush->clone().dynamicCast<KisGbrBrush>();
138
139 if (nameEdit->text().isEmpty()) {
140 resource->setName(QUuid::createUuid().toByteArray().toHex());
141 }
142 else {
143 resource->setName(name);
144 }
145
146 resource->setFilename(resource->name().split(" ").join("_") + extension);
147
149 Q_EMIT sigNewPredefinedBrush(resource);
150 }
151 }
152
153 close();
154}
155
157{
158 if (QFileInfo(m_rServer->saveLocation() + "/" + nameEdit->text().split(" ").join("_")
159 + ".gbr").exists()) {
160 buttonBox->button(QDialogButtonBox::Save)->setText(i18n("Overwrite"));
161 } else {
162 buttonBox->button(QDialogButtonBox::Save)->setText(i18n("Save"));
163 }
164}
165
166#include "moc_kis_clipboard_brush_widget.cpp"
connect(this, SIGNAL(optionsChanged()), this, SLOT(saveOptions()))
static KisBrushServerProvider * instance()
KoResourceServer< KisBrush > * brushServer()
KoResourceServer< KisBrush > * m_rServer
void sigNewPredefinedBrush(KoResourceSP)
void slotUpdateUseColorAsMask(bool useColorAsMask)
KisClipboardBrushWidget(QWidget *parent, const QString &caption, KisImageWSP image)
void showEvent(QShowEvent *) override
static KisClipboard * instance()
bool hasClip() const
QRect exactBounds() const
static bool addResourceWithUserInput(QWidget *widgetParent, KoResourceSP resource, QString storageLocation="")
QString saveLocation()
Returns path where to save user defined and imported resources to.
QSharedPointer< KisBrush > KisBrushSP
Definition kis_brush.h:49
@ ALPHAMASK
Definition kis_brush.h:39
@ IMAGESTAMP
Definition kis_brush.h:40
@ LIGHTNESSMAP
Definition kis_brush.h:41
const QString TEMPORARY_CLIPBOARD_BRUSH_NAME
const QString TEMPORARY_CLIPBOARD_BRUSH_FILENAME