Commit af7abc39 authored by noemie scherer's avatar noemie scherer Committed by Boudewijn Rempt

Add direction choice to the imagesplit plugin

One use-case for the imagesplit is modifying spritesheet found externally
(for example on the internet): splitting them then re-importing them in
the animation timeline lets you use the dedicated animation tools, like
the onionskin, and easily preview the animation with your modifications.

But most spritesheet found on the internet are horizontal, like so:

1, 2, 3
4, 5, 6

while a few are vertical, like so:

1, 3, 5
2, 4, 6

Adding a button to choose which is more appropriate lets the imagesplit
be used in both cases and still name the individual images correctly.

I put the default at horizontal, while it was at vertical before,
because it seems more widely used.

Differential revision: https://phabricator.kde.org/D18442
parent cf5346cb
......@@ -31,7 +31,6 @@
DlgImagesplit::DlgImagesplit(KisViewManager* view, const QString &suffix, QStringList listMimeFilter, int defaultMimeIndex)
: KoDialog(view->mainWindow())
{
m_page = new WdgImagesplit(this);
setCaption(i18n("Image Split"));
......@@ -84,6 +83,14 @@ bool DlgImagesplit::autoSave()
return false;
}
bool DlgImagesplit::sortHorizontal()
{
if (m_page->chkHorizontal->isChecked())
return true;
else
return false;
}
int DlgImagesplit::horizontalLines()
{
return m_page->intHorizontalSplitLines->value();
......
......@@ -42,6 +42,7 @@ public:
DlgImagesplit(KisViewManager* view, const QString &suffix, QStringList listMimeType, int defaultMimeIndex);
~DlgImagesplit() override;
bool autoSave();
bool sortHorizontal();
int horizontalLines();
int verticalLines();
int cmbIndex;
......
......@@ -102,13 +102,14 @@ void Imagesplit::slotImagesplit()
// Getting all mime types and converting them into names which are displayed at combo box
QStringList listMimeFilter = KisImportExportManager::supportedMimeTypes(KisImportExportManager::Export);
QString defaultMime = QString::fromLatin1(viewManager()->document()->mimeType());
if (defaultMime.isEmpty()) defaultMime = QString::fromLatin1(viewManager()->document()->nativeFormatMimeType());
int defaultMimeIndex = 0;
listMimeFilter.sort();
QStringList filteredMimeTypes;
QStringList listFileType;
int i = 0;
Q_FOREACH (const QString & mimeType, listMimeFilter) {
Q_FOREACH (const QString &mimeType, listMimeFilter) {
listFileType.append(KisMimeDatabase::descriptionForMimeType(mimeType));
filteredMimeTypes.append(mimeType);
if (mimeType == defaultMime) {
......@@ -137,6 +138,12 @@ void Imagesplit::slotImagesplit()
bool stop = false;
QString mimeType;
QString filepath;
QString homepath;
QString suffix;
if (dlgImagesplit->autoSave()) {
KoFileDialog dialog(viewManager()->mainWindow(), KoFileDialog::OpenDirectory, "OpenDocument");
dialog.setCaption(i18n("Save Image on Split"));
......@@ -149,48 +156,65 @@ void Imagesplit::slotImagesplit()
if (directory.isEmpty())
return;
for (int i = 0, k = 1; i < (numVerticalLines + 1); i++) {
for (int j = 0; j < (numHorizontalLines + 1); j++, k++) {
QString mimeTypeSelected = listMimeFilter.at(dlgImagesplit->cmbIndex);
QString homepath = directory.toLocalFile();
QString suffix = KisMimeDatabase::suffixesForMimeType(mimeTypeSelected).first();
qDebug() << "suffix" << suffix;
mimeType = listMimeFilter.at(dlgImagesplit->cmbIndex);
homepath = directory.toLocalFile();
suffix = KisMimeDatabase::suffixesForMimeType(mimeType).first();
if (suffix.startsWith("*.")) {
suffix = suffix.remove(0, 1);
}
qDebug() << "\tsuffix" << suffix;
if (!suffix.startsWith(".")) {
suffix = suffix.prepend('.');
}
qDebug() << "\tsuffix" << suffix;
QString fileName = dlgImagesplit->suffix() + '_' + QString::number(k) + suffix;
QString url = homepath + '/' + fileName;
if (!saveAsImage(QRect((i * img_width), (j * img_height), img_width, img_height), listMimeFilter.at(dlgImagesplit->cmbIndex), url)) {
stop = true;
break;
}
}
if (stop) {
break;
int outerLoop;
int innerLoop;
if (dlgImagesplit->sortHorizontal()) {
outerLoop = numHorizontalLines + 1;
innerLoop = numVerticalLines + 1;
}
else {
outerLoop = numVerticalLines + 1;
innerLoop = numHorizontalLines + 1;
}
for (int i = 0, k = 1; i < outerLoop; i++) {
for (int j = 0; j < innerLoop; j++, k++) {
int row;
int column;
if (dlgImagesplit->sortHorizontal()) {
row = i;
column = j;
}
else {
row = j;
column = i;
}
for (int i = 0; i < (numVerticalLines + 1); i++) {
for (int j = 0; j < (numHorizontalLines + 1); j++) {
if (dlgImagesplit->autoSave()) {
QString fileName = dlgImagesplit->suffix() + '_' + QString::number(k) + suffix;
filepath = homepath + '/' + fileName;
mimeType = listMimeFilter.at(dlgImagesplit->cmbIndex);
}
else {
KoFileDialog dialog(viewManager()->mainWindow(), KoFileDialog::SaveFile, "OpenDocument");
dialog.setCaption(i18n("Save Image on Split"));
dialog.setDefaultDir(QStandardPaths::writableLocation(QStandardPaths::PicturesLocation));
dialog.setMimeTypeFilters(listMimeFilter, defaultMime);
QUrl url = QUrl::fromUserInput(dialog.filename());
filepath = url.toLocalFile();
QString mimefilter = KisMimeDatabase::mimeTypeForFile(url.toLocalFile(), false);
mimeType = KisMimeDatabase::mimeTypeForFile(url.toLocalFile(), false);
if (url.isEmpty())
return;
if (!saveAsImage(QRect((i * img_width), (j * img_height), img_width, img_height), mimefilter, url.toLocalFile())) {
}
if (!saveAsImage(QRect((column * img_width), (row * img_height), img_width, img_height), mimeType, filepath)) {
stop = true;
break;
}
......@@ -199,9 +223,6 @@ void Imagesplit::slotImagesplit()
break;
}
}
}
}
delete dlgImagesplit;
}
......
......@@ -38,6 +38,7 @@ WdgImagesplit::WdgImagesplit(QWidget* parent)
intHorizontalSplitLines->setValue(cfg.horizontalSplitLines());
intVerticalSplitLines->setValue(cfg.verticalSplitLines());
chkHorizontal->setChecked(true);
chkAutoSave->setChecked(true);
}
......
......@@ -6,61 +6,56 @@
<rect>
<x>0</x>
<y>0</y>
<width>236</width>
<height>194</height>
<width>202</width>
<height>316</height>
</rect>
</property>
<property name="windowTitle">
<string>Image Size</string>
</property>
<layout class="QGridLayout" name="gridLayout_2">
<item row="0" column="0">
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<layout class="QFormLayout" name="formLayout">
<item row="0" column="0">
<widget class="QLabel" name="lblHorizontalSplitLines">
<property name="text">
<string>Horizontal Lines</string>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
</widget>
</item>
<item row="0" column="1">
<item row="1" column="1">
<widget class="KisIntParseSpinBox" name="intHorizontalSplitLines"/>
</item>
<item row="1" column="0">
<widget class="QLabel" name="lblVerticalSplitLines">
<property name="text">
<string>Vertical Lines</string>
<item row="3" column="1">
<widget class="KisIntParseSpinBox" name="intVerticalSplitLines"/>
</item>
<item row="4" column="0" colspan="2">
<widget class="QGroupBox" name="groupBox">
<property name="title">
<string>Sort Direction:</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="QRadioButton" name="chkHorizontal">
<property name="font">
<font>
<pointsize>10</pointsize>
</font>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
<property name="text">
<string>Hori&amp;zontal</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="KisIntParseSpinBox" name="intVerticalSplitLines"/>
</item>
</layout>
</item>
<item row="1" column="0">
<widget class="QCheckBox" name="chkAutoSave">
<item>
<widget class="QRadioButton" name="chkVertical">
<property name="font">
<font>
<pointsize>10</pointsize>
</font>
</property>
<property name="text">
<string>Autosave on Split</string>
<string>&amp;Vertical</string>
</property>
</widget>
</item>
<item row="2" column="0">
<layout class="QFormLayout" name="formLayout_2">
<item row="0" column="0">
</layout>
</widget>
</item>
<item row="5" column="0">
<widget class="QLabel" name="lblSuffix">
<property name="text">
<string>Prefix</string>
......@@ -70,7 +65,7 @@
</property>
</widget>
</item>
<item row="0" column="1">
<item row="5" column="1">
<widget class="QLineEdit" name="lineEdit">
<property name="enabled">
<bool>true</bool>
......@@ -80,7 +75,7 @@
</property>
</widget>
</item>
<item row="1" column="0">
<item row="7" column="0">
<widget class="QLabel" name="lblFileType">
<property name="text">
<string>File Type</string>
......@@ -90,12 +85,51 @@
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QComboBox" name="cmbFileType"/>
<item row="7" column="1">
<widget class="QComboBox" name="cmbFileType">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
</widget>
</item>
</layout>
<item row="9" column="0" colspan="2">
<widget class="QCheckBox" name="chkAutoSave">
<property name="font">
<font>
<pointsize>10</pointsize>
</font>
</property>
<property name="text">
<string>Autosave on Split</string>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="lblHorizontalSplitLines">
<property name="text">
<string>Horizontal Lines</string>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
</widget>
</item>
<item row="3" column="0">
<widget class="QLabel" name="lblVerticalSplitLines">
<property name="text">
<string>Vertical Lines</string>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
</widget>
</item>
</layout>
</item>
<item>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
......@@ -103,14 +137,12 @@
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>38</height>
<height>2</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
</layout>
</widget>
<customwidgets>
<customwidget>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment