14#include <QRadioButton>
21#include <QTreeWidgetItem>
23#include <QInputDialog>
24#include <QTemporaryFile>
28#include <klocalizedstring.h>
38#include <kconfiggroup.h>
42#include <ksharedconfig.h>
50 const QString &filePath,
const QPixmap &thumbnail)
51 :
m_tree(templatesResourcePath, true)
81 const QString &filePath,
const QPixmap &thumbnail, QWidget *parent)
89 setObjectName(
"template create dia" );
92 QHBoxLayout *mbox=
new QHBoxLayout( mainwidget );
93 QVBoxLayout* leftbox =
new QVBoxLayout();
94 mbox->addLayout( leftbox );
96 QLabel *label=
new QLabel(i18nc(
"Template name",
"Name:"), mainwidget);
97 QHBoxLayout *namefield=
new QHBoxLayout();
98 leftbox->addLayout( namefield );
99 namefield->addWidget(label);
100 d->
m_name=
new QLineEdit(mainwidget);
104 namefield->addWidget(
d->
m_name);
106 label=
new QLabel(i18nc(
"Group as in Template Group",
"Group:"), mainwidget);
107 leftbox->addWidget(label);
108 d->
m_groups =
new QTreeWidget(mainwidget);
116 d->
m_groups->sortItems(0, Qt::AscendingOrder);
118 QHBoxLayout *bbox=
new QHBoxLayout();
119 leftbox->addLayout( bbox );
120 d->
m_add=
new QPushButton(i18nc(
"Group as in Template Group",
"&Add Group..."), mainwidget);
122 bbox->addWidget(
d->
m_add);
123 d->
m_remove=
new QPushButton(i18n(
"&Remove"), mainwidget);
127 QVBoxLayout *rightbox=
new QVBoxLayout();
128 mbox->addLayout( rightbox );
129 QGroupBox *pixbox =
new QGroupBox(i18n(
"Picture"), mainwidget);
130 rightbox->addWidget(pixbox);
131 QVBoxLayout *pixlayout=
new QVBoxLayout(pixbox );
132 d->
m_default=
new QRadioButton(i18n(
"&Preview"), pixbox);
136 QHBoxLayout *custombox=
new QHBoxLayout();
137 d->
m_custom=
new QRadioButton(i18n(
"Custom:"), pixbox);
141 d->
m_select=
new QPushButton(i18n(
"&Select..."), pixbox);
144 custombox->addStretch(1);
145 pixlayout->addLayout(custombox);
147 pixlayout->addWidget(
d->
m_preview, 0, Qt::AlignCenter);
148 pixlayout->addStretch(1);
150 d->
m_defaultTemplate =
new QCheckBox( i18n(
"Use the new template as default"), mainwidget );
153 d->
m_defaultTemplate->setToolTip(i18n(
"Use the new template every time Krita starts"));
172 const QTreeWidgetItem* item =
d->
m_groups->currentItem();
177 if ( item->parent() != 0 )
179 d->
m_name->setText( item->text( 0 ) );
190 QTemporaryFile tempFile;
191 if (!tempFile.open()) {
192 qWarning(
"Creation of temporary file to store template failed.");
195 fileName = tempFile.fileName();
200 qWarning(
"Could not save template");
214 QImage icon = pixmap.toImage();
215 icon.convertTo(QImage::Format_ARGB32);
216 const int iconExtent = qMax(icon.width(), icon.height());
217 icon = icon.copy((icon.width() - iconExtent) / 2, (icon.height() - iconExtent) / 2, iconExtent, iconExtent);
218 icon.save(fileName,
"PNG");
224 QTreeWidgetItem *item =
d->
m_groups->currentItem();
233 if (item->parent() != 0)
248 if (
d->
m_name->text().isEmpty()) {
256 dir += group->
name();
257 QString templateDir = dir+
"/.source/";
258 QString iconDir = dir+
"/.icon/";
261 QString tmpIcon =
".icon/"+file;
263 QString icon=iconDir+file;
266 QString ext =
".kra";
268 QString dest = templateDir + file + ext;
269 if (QFile::exists(dest)) {
271 file = file.prepend(
'_' );
272 dest = templateDir + file + ext;
273 tmpIcon=
".icon/" + file +
".png";
274 icon=iconDir + file +
".png";
276 while (QFile(dest).exists());
280 if (!group->
add(t)) {
282 if (existingTemplate && !existingTemplate->
isHidden()) {
283 if (QMessageBox::warning(
this,
284 i18nc(
"@title:window",
"Krita"),
285 i18n(
"Do you really want to overwrite the existing '%1' template?", existingTemplate->
name()),
286 QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes) == QMessageBox::Yes) {
300 if (!path.mkpath(templateDir) || !path.mkpath(iconDir)) {
311 qWarning() <<
"Could not copy" <<
d->
m_filePath <<
"to" << dest;
320 warnUI <<
"Could not save the preview picture!";
327 for(QStringList::ConstIterator it=
tmp.constBegin(); it!=
tmp.constEnd() && !ready; ++it) {
328 if ((*it).contains(dir)==0) {
329 orig = (*it) +
".directory";
331 if (QFile(orig).exists()) {
332 dest = dir +
"/.directory";
334 if (!QFile(orig).copy(dest)) {
335 warnKrita <<
"Failed to copy from" << orig <<
"to" << dest;
347 KConfigGroup grp( KSharedConfig::openConfig(),
"TemplateChooserDialog");
348 grp.writeEntry(
"LastReturnType",
"Template" );
349 grp.writePathEntry(
"FullTemplateName", dir +
'/' + t->
file() );
350 grp.writePathEntry(
"AlwaysUseTemplate", dir +
'/' + t->
file() );
377 dlg.
setDefaultDir(QStandardPaths::writableLocation(QStandardPaths::PicturesLocation));
389 if (image.isNull()) {
390 QMessageBox::warning(
this, i18nc(
"@title:window",
"Krita"), i18n(
"%1 is not a valid image file!", fn));
407 const QString name = QInputDialog::getText(
this, i18nc(
"Group as in Template Group",
"Add Group"), i18nc(
"Group as in Template Group",
"Enter group name:"));
410 QMessageBox::information(
this, i18n(
"This name has already been used."), i18nc(
"Group as in Template Group",
"Add Group") );
419 d->
m_groups->sortItems(0, Qt::AscendingOrder);
427 QTreeWidgetItem *item =
d->
m_groups->currentItem();
433 if (item->parent() == 0) {
434 what = i18nc(
"Group as in Template Group",
"Do you really want to remove that group?");
435 removed = i18nc(
"@title:window",
"Remove Group");
437 what = i18n(
"Do you really want to remove that template?");
438 removed = i18nc(
"@title:window",
"Remove Template");
441 if (QMessageBox::warning(
this,
444 QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes) == QMessageBox:: No) {
449 if (item->parent() == 0) {
458 for(; it != groups.constEnd() && !done; ++it) {
488 warnUI <<
"Trying to load picture";
495 d->
m_preview->setText(i18n(
"Could not load picture."));
499 d->
m_preview->setText(i18n(
"No picture available."));
QList< QString > QStringList
static void saveAsQuadraticPng(const QPixmap &pixmap, const QString &fileName)
static const int thumbnailExtent
connect(this, SIGNAL(optionsChanged()), this, SLOT(saveOptions()))
static QByteArray nativeFormatMimeType()
QCheckBox * m_defaultTemplate
KisTemplateCreateDiaPrivate(const QString &templatesResourcePath, const QString &filePath, const QPixmap &thumbnail)
KisTemplateCreateDiaPrivate *const d
KisTemplateCreateDia(const QString &templatesResourcePath, const QString &filePath, const QPixmap &thumbnail, QWidget *parent=0)
void slotSelectionChanged()
~KisTemplateCreateDia() override
void slotNameChanged(const QString &name)
static void createTemplate(const QString &templatesResourcePath, const char *suffix, KisDocument *document, QWidget *parent=0)
KisTemplate * find(const QString &name) const
QList< KisTemplate * > templates() const
bool isHidden() const
If all children are hidden, we are hidden too.
bool add(KisTemplate *t, bool force=false, bool touch=true)
void setHidden(bool hidden=true) const
if we should hide, we hide all the children
QString templatesResourcePath() const
bool add(KisTemplateGroup *g)
KisTemplateGroup * find(const QString &name) const
QList< KisTemplateGroup * > groups() const
void setHidden(bool hidden=true)
const QPixmap & loadPicture()
A dialog base class with standard buttons and predefined layouts.
virtual void slotButtonClicked(int button)
void enableButtonOk(bool state)
virtual void setCaption(const QString &caption)
void setButtons(ButtonCodes buttonMask)
void setDefaultButton(ButtonCode id)
@ Ok
Show Ok button. (this button accept()s the dialog; result set to QDialog::Accepted)
@ Cancel
Show Cancel-button. (this button reject()s the dialog; result set to QDialog::Rejected)
void setImageFilters()
setImageFilters sets the name filters for the file dialog to all image formats Qt's QImageReader supp...
QString filename()
Get the file name the user selected in the file dialog.
void setDefaultDir(const QString &defaultDir, bool force=false)
setDefaultDir set the default directory to defaultDir.
void setCaption(const QString &caption)
static QString saveLocation(const QString &type, const QString &suffix=QString(), bool create=true)
typedef void(QOPENGLF_APIENTRYP PFNGLINVALIDATEBUFFERDATAPROC)(GLuint buffer)
QString trimmed(const QString &string)