Krita Source Code Documentation
Loading...
Searching...
No Matches
KisCustomPattern Class Reference

#include <kis_custom_pattern.h>

+ Inheritance diagram for KisCustomPattern:

Signals

void activatedResource (KoResourceSP)
 
void patternAdded (KoResourceSP)
 
void patternUpdated (KoResourceSP)
 

Public Member Functions

 KisCustomPattern (QWidget *parent, const char *name, const QString &caption, KisViewManager *view)
 
 ~KisCustomPattern () override
 
- Public Member Functions inherited from KisWdgCustomPattern
 KisWdgCustomPattern (QWidget *parent, const char *name)
 

Private Slots

void slotAddPredefined ()
 
void slotUpdateCurrentPattern ()
 
void slotUsePattern ()
 

Private Member Functions

void createPattern ()
 

Private Attributes

KoPatternSP m_pattern
 
KoResourceServer< KoPattern > * m_rServer {0}
 
KisViewManagerm_view {0}
 

Detailed Description

Definition at line 32 of file kis_custom_pattern.h.

Constructor & Destructor Documentation

◆ KisCustomPattern()

KisCustomPattern::KisCustomPattern ( QWidget * parent,
const char * name,
const QString & caption,
KisViewManager * view )

Definition at line 33 of file kis_custom_pattern.cc.

34 : KisWdgCustomPattern(parent, name)
35 , m_view(view)
36{
37 Q_ASSERT(m_view);
38 setWindowTitle(caption);
39
40 m_pattern = 0;
41
42 preview->setScaledContents(true);
43
45
46 connect(addButton, SIGNAL(pressed()), this, SLOT(slotAddPredefined()));
47 connect(patternButton, SIGNAL(pressed()), this, SLOT(slotUsePattern()));
48 connect(updateButton, SIGNAL(pressed()), this, SLOT(slotUpdateCurrentPattern()));
49 connect(cmbSource, SIGNAL(currentIndexChanged(int)), this, SLOT(slotUpdateCurrentPattern()));
50
51 lblWarning->setVisible(false);
53}
connect(this, SIGNAL(optionsChanged()), this, SLOT(saveOptions()))
KisViewManager * m_view
KoResourceServer< KoPattern > * m_rServer
KisWdgCustomPattern(QWidget *parent, const char *name)
static KoResourceServerProvider * instance()
KoResourceServer< KoPattern > * patternServer

References connect(), KoResourceServerProvider::instance(), m_pattern, m_rServer, m_view, KoResourceServerProvider::patternServer, slotAddPredefined(), slotUpdateCurrentPattern(), and slotUsePattern().

◆ ~KisCustomPattern()

KisCustomPattern::~KisCustomPattern ( )
override

Definition at line 55 of file kis_custom_pattern.cc.

56{
57 m_pattern.clear();
58}

References m_pattern.

Member Function Documentation

◆ activatedResource

void KisCustomPattern::activatedResource ( KoResourceSP )
signal

◆ createPattern()

void KisCustomPattern::createPattern ( )
private

Definition at line 163 of file kis_custom_pattern.cc.

164{
165 if (!m_view) return;
166
168 KisPaintDeviceSP cache;
169 QString name;
170 KisImageWSP image = m_view->image();
171 if (!image) return;
172 QRect rc = image->bounds();
173
174 if (cmbSource->currentIndex() == 0) {
175 dev = m_view->activeNode()->projection();
176 name = m_view->activeNode()->name();
177 QRect rc2 = dev->exactBounds();
178 rc = rc.intersected(rc2);
179 }
180 else {
181 image->barrierLock();
182 dev = image->projection();
183 image->unlock();
184 name = image->objectName();
185 }
186 if (!dev) return;
187
188 if(m_view->selection()) {
189 KisSelectionSP selection = m_view->selection();
190 QRect selectionRect = selection->selectedExactRect();
191 cache = dev->createCompositionSourceDevice();
192 KisPainter gc(cache);
193 gc.setSelection(selection);
194 gc.bitBlt(selectionRect.topLeft(), dev, selectionRect);
195 rc = selectionRect;
196 } else {
197 cache = dev;
198 }
199 if (!cache) return;
200
201
202 // warn when creating large patterns
203
204 QSize size = rc.size();
205 if (size.height() > 1000 || size.width() > 1000) {
206 lblWarning->setVisible(true);
207 size.scale(1000, 1000, Qt::KeepAspectRatio);
208 }
209 else {
210 lblWarning->setVisible(false);
211 }
212
214 m_pattern = KoPatternSP(new KoPattern(cache->createThumbnail(size.width(), size.height(), rc, /*oversample*/ 1,
217}
QSharedPointer< KoPattern > KoPatternSP
Definition KoPattern.h:16
void unlock()
Definition kis_image.cc:805
void barrierLock(bool readOnly=false)
Wait until all the queued background jobs are completed and lock the image.
Definition kis_image.cc:756
KisPaintDeviceSP projection() const
QRect bounds() const override
KisPaintDeviceSP createCompositionSourceDevice() const
QImage createThumbnail(qint32 maxw, qint32 maxh, QRect rect, qreal oversample=1, KoColorConversionTransformation::Intent renderingIntent=KoColorConversionTransformation::internalRenderingIntent(), KoColorConversionTransformation::ConversionFlags conversionFlags=KoColorConversionTransformation::internalConversionFlags())
QRect exactBounds() const
KisNodeSP activeNode()
KisSelectionSP selection()
KisImageWSP image() const
Return the image this view is displaying.
Write API docs here.
Definition KoPattern.h:21
QString saveLocation()
Returns path where to save user defined and imported resources to.
const char * name(StandardAction id)
int size(const Forest< T > &forest)
Definition KisForest.h:1232
virtual KisPaintDeviceSP projection() const =0
QString name() const
QRect selectedExactRect() const
Slow, but exact way of determining the rectangle that encloses the selection.

References KisViewManager::activeNode(), KisImage::barrierLock(), KisPainter::bitBlt(), KisImage::bounds(), KisPaintDevice::createCompositionSourceDevice(), KisPaintDevice::createThumbnail(), KisPaintDevice::exactBounds(), KisViewManager::image(), KoResourceServerProvider::instance(), KoColorConversionTransformation::internalConversionFlags(), KoColorConversionTransformation::internalRenderingIntent(), m_pattern, m_view, KisBaseNode::name(), KoResourceServerProvider::patternServer, KisImage::projection(), KisBaseNode::projection(), KoResourceServer< T >::saveLocation(), KisSelection::selectedExactRect(), KisViewManager::selection(), KisPainter::setSelection(), and KisImage::unlock().

◆ patternAdded

void KisCustomPattern::patternAdded ( KoResourceSP )
signal

◆ patternUpdated

void KisCustomPattern::patternUpdated ( KoResourceSP )
signal

◆ slotAddPredefined

void KisCustomPattern::slotAddPredefined ( )
privateslot

Definition at line 92 of file kis_custom_pattern.cc.

93{
94 if (!m_pattern) return;
95
96 // Save in the directory that is likely to be: ~/.kde/share/apps/krita/patterns
97 // a unique file with this pattern name
99
100 KoFileDialog dlg(this, KoFileDialog::SaveFile, "KisCustomPattern::slotAddPredefined");
101 dlg.setDefaultDir(dir + "/" + m_pattern->name() + ".pat");
102 dlg.setMimeTypeFilters(KisResourceLoaderRegistry::instance()->mimeTypes(ResourceType::Patterns));
103 dlg.setCaption(i18n("Add to Predefined Patterns"));
104
105 QString filename = dlg.filename();
106
107 if (filename == "") {
108 // dialog was cancelled
109 return;
110 }
111 bool hadToChangeFilename = false;
112
113 QFileInfo fi(filename);
114 if (fi.suffix().isEmpty()) {
115 fi.setFile(fi.baseName() + m_pattern->defaultFileExtension());
116 hadToChangeFilename = true;
117 }
118
119 if (fi.baseName() != m_pattern->name()) {
120 m_pattern->setName(fi.baseName());
121 }
122
123 bool overwrite = false;
124 if (fi.exists()) {
125 if (hadToChangeFilename) { // if not, the File Dialog would show the warning
126 if (QMessageBox::warning(this, i18nc("@title:window", "Krita"), i18n("This pattern already exists. Do you want to overwrite it?"), QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes) {
127 overwrite = true;
128 }
129 } else { // the File Dialog showed the warning and the user said "yeah fine"
130 overwrite = true;
131 }
132 }
133
134 if (!filename.isEmpty()) {
135 m_pattern->setFilename(fi.fileName()); // to make sure we include the suffix added earlier
136 if (!fi.exists()) {
137 if (!KisResourceUserOperations::addResourceWithUserInput(this, m_pattern->clone().dynamicCast<KoPattern>())) {
138 qWarning() << "Could not add pattern with filename" << filename;
139 }
140 else {
141 Q_EMIT patternAdded(m_pattern);
142 }
143 }
144 else if (overwrite) {
146 qWarning() << "Could not add pattern with filename" << filename;
147 }
148 else {
150 }
151 }
152 }
153}
void patternAdded(KoResourceSP)
void patternUpdated(KoResourceSP)
static KisResourceLoaderRegistry * instance()
static bool addResourceWithUserInput(QWidget *widgetParent, KoResourceSP resource, QString storageLocation="")
static bool updateResourceWithUserInput(QWidget *widgetParent, KoResourceSP resource)
const QString Patterns

References KisResourceUserOperations::addResourceWithUserInput(), KoFileDialog::filename(), KisResourceLoaderRegistry::instance(), KoResourceServerProvider::instance(), m_pattern, patternAdded(), ResourceType::Patterns, KoResourceServerProvider::patternServer, patternUpdated(), KoFileDialog::SaveFile, KoResourceServer< T >::saveLocation(), KoFileDialog::setCaption(), KoFileDialog::setDefaultDir(), KoFileDialog::setMimeTypeFilters(), and KisResourceUserOperations::updateResourceWithUserInput().

◆ slotUpdateCurrentPattern

void KisCustomPattern::slotUpdateCurrentPattern ( )
privateslot

Definition at line 60 of file kis_custom_pattern.cc.

61{
62 m_pattern.clear();
63 if (m_view && m_view->image()) {
65 if (m_pattern) {
66 const qint32 maxSize = 150;
67 if ((m_pattern->width() > maxSize) || (m_pattern->height() > maxSize)) {
68 float aspectRatio = (float)m_pattern->width() / m_pattern->height();
69 qint32 scaledWidth, scaledHeight;
70
71 if (m_pattern->width() > m_pattern->height()) {
72 scaledWidth = maxSize;
73 scaledHeight = maxSize / aspectRatio;
74 } else {
75 scaledWidth = maxSize * aspectRatio;
76 scaledHeight = maxSize;
77 }
78
79 if (scaledWidth == 0) scaledWidth++;
80 if (scaledHeight == 0) scaledHeight++;
81
82 QPixmap scaledPixmap = QPixmap::fromImage(m_pattern->pattern());
83 preview->setPixmap(scaledPixmap.scaled(scaledWidth, scaledHeight, Qt::KeepAspectRatio, Qt::SmoothTransformation));
84 } else {
85 preview->setPixmap(QPixmap::fromImage(m_pattern->pattern()));
86 }
87 }
88 }
89
90}

References createPattern(), KisViewManager::image(), m_pattern, and m_view.

◆ slotUsePattern

void KisCustomPattern::slotUsePattern ( )
privateslot

Definition at line 155 of file kis_custom_pattern.cc.

156{
157 if (!m_pattern)
158 return;
159 KoPatternSP copy = m_pattern->clone().dynamicCast<KoPattern>();
160 emit(activatedResource(copy));
161}
void activatedResource(KoResourceSP)

References activatedResource(), and m_pattern.

Member Data Documentation

◆ m_pattern

KoPatternSP KisCustomPattern::m_pattern
private

Definition at line 52 of file kis_custom_pattern.h.

◆ m_rServer

KoResourceServer<KoPattern>* KisCustomPattern::m_rServer {0}
private

Definition at line 53 of file kis_custom_pattern.h.

53{0};

◆ m_view

KisViewManager* KisCustomPattern::m_view {0}
private

Definition at line 51 of file kis_custom_pattern.h.

51{0};

The documentation for this class was generated from the following files: