Skip to content

Commit

Permalink
Editor: Make the editable ComboBox with available categories
Browse files Browse the repository at this point in the history
Note: The order weight for the category is a WIP, it must change the order of categories in the tilesets item box
Part of #225
  • Loading branch information
Wohlstand committed May 29, 2018
1 parent 5cbaee6 commit ede98fb
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 37 deletions.
96 changes: 90 additions & 6 deletions Editor/tools/tilesets/tilesetgroupeditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include <QInputDialog>
#include <QtDebug>
#include <QSet>

#include <common_features/main_window_ptr.h>
#include <common_features/util.h>
Expand Down Expand Up @@ -71,6 +72,7 @@ TilesetGroupEditor::TilesetGroupEditor(QGraphicsScene *scene, QWidget *parent) :

TilesetGroupEditor::~TilesetGroupEditor()
{
m_categories.reset();
delete layout;
delete ui;
}
Expand All @@ -79,7 +81,7 @@ SimpleTilesetGroup TilesetGroupEditor::toSimpleTilesetGroup()
{
SimpleTilesetGroup s;
s.groupName = ui->tilesetGroupName->text();
s.groupCat = ui->category->text();
s.groupCat = ui->category->currentText();
s.groupWeight = ui->orderWeight->value();
for(int i = 0; i < tilesets.size(); ++i)
s.tilesets << tilesets[i].first;
Expand All @@ -104,8 +106,6 @@ void TilesetGroupEditor::SaveSimpleTilesetGroup(const QString &path, const Simpl
for(int i = 1; i < tilesetGroup.tilesets.size() + 1; ++i)
simpleTilesetGroupINI.setValue(QString("tileset-%1").arg(i), tilesetGroup.tilesets[i - 1]);
simpleTilesetGroupINI.endGroup();

lastFileName = QFileInfo(path).baseName();
}

bool TilesetGroupEditor::OpenSimpleTilesetGroup(const QString &path, SimpleTilesetGroup &tilesetGroup)
Expand All @@ -129,7 +129,7 @@ bool TilesetGroupEditor::OpenSimpleTilesetGroup(const QString &path, SimpleTiles
tilesetGroup.tilesets << simpleTilesetINI.value(QString("tileset-%1").arg(i)).toString();
}
simpleTilesetINI.endGroup();
lastFileName = QFileInfo(path).baseName();

return true;
}

Expand Down Expand Up @@ -220,15 +220,27 @@ void TilesetGroupEditor::on_Open_clicked()
{
tilesets.clear();
ui->tilesetGroupName->setText(t.groupName);
ui->category->setText(t.groupCat);
ui->orderWeight->setValue(t.groupWeight);
QFileInfo pathInfo(f);
lastFileName = pathInfo.baseName();
QString dirPath = pathInfo.absoluteDir().absolutePath();
m_categories.reset(new QSettings(dirPath + "/categories.ini", QSettings::IniFormat, this));

for(QString &tarName : t.tilesets)
{
QString rootTilesetDir = m_configs->config_dir + "tilesets/";
SimpleTileset st;
if(tileset::OpenSimpleTileset(rootTilesetDir + tarName, st))
tilesets << qMakePair<QString, SimpleTileset>(tarName, st);
}
fetchCategories(dirPath);
ui->category->setCurrentText(t.groupCat);
if(m_categories)
{
m_categories->beginGroup(categoryName(t.groupCat));
ui->categoryWeight->setValue(m_categories->value("weight", -1).toInt());
m_categories->endGroup();
}
redrawAll();
}
else
Expand All @@ -254,7 +266,13 @@ void TilesetGroupEditor::on_Save_clicked()
if(!fileName.endsWith(".tsgrp.ini"))
fileName += ".tsgrp.ini";

SaveSimpleTilesetGroup(m_configs->config_dir + "group_tilesets/" + fileName, toSimpleTilesetGroup());
QString path = m_configs->config_dir + "group_tilesets/" + fileName;
SimpleTilesetGroup g = toSimpleTilesetGroup();
SaveSimpleTilesetGroup(path, g);
QFileInfo pathInfo(path);
lastFileName = pathInfo.baseName();
fetchCategories(pathInfo.absoluteDir().absolutePath());
ui->category->setCurrentText(g.groupCat);
}

void TilesetGroupEditor::redrawAll()
Expand Down Expand Up @@ -320,6 +338,71 @@ void TilesetGroupEditor::on_tilesetDown_clicked()
}
}

QString TilesetGroupEditor::categoryName(QString catName)
{
return catName.toLower().replace(' ', '_');
}

void TilesetGroupEditor::fetchCategories(QString path)
{
ui->category->clear();
QDir groups(path);
if(!groups.exists())
return;

QStringList filters;
filters << "*.tsgrp.ini";
QStringList files = groups.entryList(filters);

QSet<QString> categoryNames;
for(QString &file : files)
{
SimpleTilesetGroup xxx;
if(TilesetGroupEditor::OpenSimpleTilesetGroup(path + "/" + file, xxx))
categoryNames.insert(xxx.groupCat);
}
for(const QString &cat : categoryNames)
{
if(m_categories)
{
m_categories->beginGroup(categoryName(cat));
m_categories->setValue("name", cat);
m_categories->endGroup();
}
ui->category->addItem(cat);
}
}

void TilesetGroupEditor::on_categoryWeight_editingFinished()
{
if(m_categories)
{
m_categories->beginGroup(categoryName(ui->category->currentText()));
m_categories->setValue("weight", ui->categoryWeight->value());
m_categories->endGroup();
}
}

void TilesetGroupEditor::on_category_currentIndexChanged(const QString &arg1)
{
if(m_categories)
{
m_categories->beginGroup(categoryName(arg1));
ui->categoryWeight->setValue(m_categories->value("weight", -1).toInt());
m_categories->endGroup();
}
}

void TilesetGroupEditor::on_category_editTextChanged(const QString &arg1)
{
if(m_categories)
{
m_categories->beginGroup(categoryName(arg1));
ui->categoryWeight->setValue(m_categories->value("weight", -1).toInt());
m_categories->endGroup();
}
}

void TilesetGroupEditor::movedTileset(const QModelIndex &sourceParent, int sourceStart, int sourceEnd, const QModelIndex &destinationParent, int destinationRow)
{
Q_UNUSED(sourceParent)
Expand All @@ -332,3 +415,4 @@ void TilesetGroupEditor::movedTileset(const QModelIndex &sourceParent, int sourc
redrawAll();
ui->tilesetList->setCurrentRow(destinationRow);
}

18 changes: 14 additions & 4 deletions Editor/tools/tilesets/tilesetgroupeditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#define TILESETGROUPEDITOR_H

#include <QDialog>
#include <QSharedPointer>

#include <common_features/flowlayout.h>
#include <data_configs/obj_tilesets.h>
Expand All @@ -14,6 +15,7 @@ class TilesetGroupEditor;
}

class dataconfigs;
class QSettings;
class TilesetGroupEditor : public QDialog
{
Q_OBJECT
Expand All @@ -26,6 +28,8 @@ class TilesetGroupEditor : public QDialog
static void SaveSimpleTilesetGroup(const QString &path, const SimpleTilesetGroup &tileset);
static bool OpenSimpleTilesetGroup(const QString &path, SimpleTilesetGroup &tileset);

static QString categoryName(QString catName);

private slots:
void on_addTileset_clicked();
void on_RemoveTileset_clicked();
Expand All @@ -34,17 +38,23 @@ private slots:
void on_Save_clicked();
void on_tilesetUp_clicked();
void on_tilesetDown_clicked();
void on_category_currentIndexChanged(const QString &arg1);
void on_category_editTextChanged(const QString &arg1);
void on_categoryWeight_editingFinished();

void movedTileset( const QModelIndex & sourceParent, int sourceStart, int sourceEnd, const QModelIndex & destinationParent, int destinationRow );

private:
void fetchCategories(QString path);

static QString lastFileName;
FlowLayout * layout;
Ui::TilesetGroupEditor *ui;
FlowLayout *layout = nullptr;
QSharedPointer<QSettings> m_categories;
Ui::TilesetGroupEditor *ui = nullptr;
void redrawAll();
QGraphicsScene * scn;
QGraphicsScene *scn = nullptr;
QList<QPair<QString,SimpleTileset> > tilesets;
dataconfigs* m_configs;
dataconfigs *m_configs = nullptr;
};

#endif // TILESETGROUPEDITOR_H
71 changes: 44 additions & 27 deletions Editor/tools/tilesets/tilesetgroupeditor.ui
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,23 @@
</item>
<item row="1" column="4" rowspan="5" colspan="2">
<layout class="QGridLayout" name="gridLayout_3">
<item row="5" column="0">
<widget class="QLabel" name="label_4">
<property name="text">
<string>Order weight</string>
<item row="3" column="0">
<widget class="QSpinBox" name="orderWeight">
<property name="toolTip">
<string>Defines the custom order priority. If weight values are equal between of different tileset group or equal to -1, tileset groups will be ordered alphabetically.</string>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QLabel" name="label_2">
<property name="text">
<string>Tileset group name:</string>
<property name="minimum">
<number>-1</number>
</property>
<property name="maximum">
<number>99999999</number>
</property>
<property name="value">
<number>-1</number>
</property>
</widget>
</item>
<item row="7" column="0">
<item row="9" column="0">
<spacer name="verticalSpacer_3">
<property name="orientation">
<enum>Qt::Vertical</enum>
Expand All @@ -57,24 +59,24 @@
</property>
</spacer>
</item>
<item row="3" column="0">
<widget class="QLabel" name="label_3">
<property name="text">
<string>TextLabel</string>
</property>
</widget>
<item row="1" column="0">
<widget class="QLineEdit" name="tilesetGroupName"/>
</item>
<item row="2" column="0">
<item row="4" column="0">
<widget class="QLabel" name="categoryLabel">
<property name="text">
<string>Category:</string>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLineEdit" name="tilesetGroupName"/>
<item row="5" column="0">
<widget class="QComboBox" name="category">
<property name="editable">
<bool>true</bool>
</property>
</widget>
</item>
<item row="8" column="0">
<item row="10" column="0">
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
Expand All @@ -87,14 +89,22 @@
</property>
</spacer>
</item>
<item row="4" column="0">
<widget class="QLineEdit" name="category"/>
<item row="2" column="0">
<widget class="QLabel" name="orderWeightLabel">
<property name="text">
<string>Order weight</string>
</property>
</widget>
</item>
<item row="6" column="0">
<widget class="QSpinBox" name="orderWeight">
<property name="toolTip">
<string>Defines the custom order priority. If weight values are equal between of different tileset group or equal to -1, tileset groups will be ordered alphabetically.</string>
<item row="0" column="0">
<widget class="QLabel" name="groupTitleLabel">
<property name="text">
<string>Tileset group name:</string>
</property>
</widget>
</item>
<item row="7" column="0">
<widget class="QSpinBox" name="categoryWeight">
<property name="minimum">
<number>-1</number>
</property>
Expand All @@ -106,6 +116,13 @@
</property>
</widget>
</item>
<item row="6" column="0">
<widget class="QLabel" name="categoryWeightLabel">
<property name="text">
<string>Category order weight</string>
</property>
</widget>
</item>
</layout>
</item>
<item row="2" column="6">
Expand Down

0 comments on commit ede98fb

Please sign in to comment.