WIP string editor UI

This commit is contained in:
Aruki
2018-12-26 23:59:59 -07:00
parent 0ae7b8686e
commit 76bc2b50f8
24 changed files with 853 additions and 209 deletions

View File

@@ -0,0 +1,68 @@
#include "CStringDelegate.h"
#include <QPainter>
/** Constants */
static constexpr int gkMargin = 3;
static constexpr int gkSpacing = 3;
CStringDelegate::CStringDelegate(QObject* pParent /*= 0*/)
: CCustomDelegate(pParent)
{}
SDelegateFontInfo CStringDelegate::GetFontInfo(const QStyleOptionViewItem& rkOption) const
{
SDelegateFontInfo Info = CCustomDelegate::GetFontInfo(rkOption);
Info.NameFont.setPointSize( rkOption.font.pointSize() );
Info.NameFontMetrics = QFontMetrics(Info.NameFont);
Info.NamePen.setColor( Info.NamePen.color().darker(120.f) );
Info.InfoFont.setPointSize( rkOption.font.pointSize() );
Info.InfoFont.setItalic(true);
Info.InfoFontMetrics = QFontMetrics(Info.InfoFont);
Info.InfoPen = QPen(rkOption.palette.text(), 1.f);
return Info;
}
QSize CStringDelegate::sizeHint(const QStyleOptionViewItem& kOption, const QModelIndex& kIndex) const
{
QSize BaseSize = CCustomDelegate::sizeHint(kOption, kIndex);
SDelegateFontInfo FontInfo = GetFontInfo(kOption);
int Height = (gkMargin * 2) + gkSpacing + (FontInfo.NameFontMetrics.height() * 2);
return QSize(BaseSize.rwidth(), Height);
}
void CStringDelegate::paint(QPainter* pPainter, const QStyleOptionViewItem& kOption, const QModelIndex& kIndex) const
{
SDelegateFontInfo FontInfo = GetFontInfo(kOption);
QString StringName = kIndex.model()->data(kIndex, Qt::DisplayRole).toString();
QString StringText = kIndex.model()->data(kIndex, Qt::UserRole).toString();
// Calculate rects
int X = kOption.rect.left() + gkMargin;
int Width = kOption.rect.width() - (gkMargin * 2);
int NameHeight = FontInfo.NameFontMetrics.height();
int TextHeight = FontInfo.InfoFontMetrics.height();
int NameY = kOption.rect.top() + gkMargin;
int TextY = NameY + NameHeight + gkSpacing;
QRect NameRect(X, NameY, Width, NameHeight);
QRect TextRect(X, TextY, Width, TextHeight);
// Elide name/text
StringName = FontInfo.NameFontMetrics.elidedText(StringName, Qt::ElideRight, Width);
StringText = FontInfo.InfoFontMetrics.elidedText(StringText, Qt::ElideRight, Width);
// Draw selection rect
if (kOption.state & QStyle::State_Selected)
{
pPainter->fillRect( kOption.rect, kOption.palette.highlight() );
}
// Draw string info
pPainter->setFont(FontInfo.NameFont);
pPainter->setPen(FontInfo.NamePen);
pPainter->drawText(NameRect, Qt::AlignLeft, StringName);
pPainter->setFont(FontInfo.InfoFont);
pPainter->setPen(FontInfo.InfoPen);
pPainter->drawText(TextRect, Qt::AlignLeft, StringText);
}

View File

@@ -0,0 +1,17 @@
#ifndef CSTRINGDELEGATE_H
#define CSTRINGDELEGATE_H
#include "Editor/CCustomDelegate.h"
/** Delegate for rendering string entries in the string editor list view */
class CStringDelegate : public CCustomDelegate
{
public:
CStringDelegate(QObject* pParent = 0);
SDelegateFontInfo GetFontInfo(const QStyleOptionViewItem& rkOption) const;
QSize sizeHint(const QStyleOptionViewItem& kOption, const QModelIndex& kIndex) const;
void paint(QPainter* pPainter, const QStyleOptionViewItem& kOption, const QModelIndex& kIndex) const;
};
#endif // CSTRINGDELEGATE_H

View File

@@ -0,0 +1,132 @@
#include "CStringEditor.h"
#include "ui_CStringEditor.h"
#include "CStringDelegate.h"
#include "Editor/UICommon.h"
#include "Editor/Widgets/CSizeableTabBar.h"
#include <QSettings>
/** Settings strings */
const char* gkpLanguageSetting = "StringEditor/EditLanguage";
/** Constructor */
CStringEditor::CStringEditor(CStringTable* pStringTable, QWidget* pParent)
: IEditor(pParent)
, mpUI(new Ui::CStringEditor)
, mpStringTable(pStringTable)
, mCurrentLanguage(ELanguage::English)
, mCurrentStringIndex(0)
{
mpListModel = new CStringListModel(pStringTable, this);
mpUI->setupUi(this);
InitUI();
LoadSettings();
UpdateUI();
}
CStringEditor::~CStringEditor()
{
delete mpUI;
}
void CStringEditor::InitUI()
{
mpUI->StringNameListView->setModel(mpListModel);
mpUI->StringNameListView->setItemDelegate( new CStringDelegate(this) );
#if 0
// Set up language combo box
for (uint LanguageIdx = 0; LanguageIdx < mpStringTable->NumLanguages(); LanguageIdx++)
{
ELanguage Language = mpStringTable->LanguageByIndex(LanguageIdx);
const char* pkLanguageName = TEnumReflection<ELanguage>::ConvertValueToString(Language);
mpUI->LanguageComboBox->addItem(pkLanguageName);
}
#else
QTabBar* pTabBar = new QTabBar(this);
pTabBar->setExpanding(false);
// Set up language combo box
for (uint LanguageIdx = 0; LanguageIdx < mpStringTable->NumLanguages(); LanguageIdx++)
{
ELanguage Language = mpStringTable->LanguageByIndex(LanguageIdx);
const char* pkLanguageName = TEnumReflection<ELanguage>::ConvertValueToString(Language);
pTabBar->addTab(pkLanguageName);
}
QVBoxLayout* pTabLayout = new QVBoxLayout(mpUI->TabsContainerWidget);
pTabLayout->setContentsMargins(0,0,0,0);
pTabLayout->addWidget(pTabBar);
#endif
// Connect signals & slots
connect( mpUI->StringNameListView->selectionModel(), SIGNAL(currentChanged(QModelIndex,QModelIndex)),
this, SLOT(OnStringSelected(QModelIndex)) );
// connect( mpUI->LanguageComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(OnLanguageChanged(int)) );
connect( pTabBar, SIGNAL(currentChanged(int)), this, SLOT(OnLanguageChanged(int)) );
// Update window title
QString WindowTitle = "%APP_FULL_NAME% - String Editor - %1[*]";
WindowTitle = WindowTitle.arg( TO_QSTRING(mpStringTable->Entry()->CookedAssetPath(true).GetFileName()) );
SET_WINDOWTITLE_APPVARS(WindowTitle);
// Update status bar
QString StatusText = QString("%1 languages, %2 strings")
.arg(mpStringTable->NumLanguages())
.arg(mpStringTable->NumStrings());
mpUI->StatusBar->setStatusTip(StatusText);
}
void CStringEditor::UpdateUI()
{
}
void CStringEditor::SetActiveLanguage(ELanguage Language)
{
if (mCurrentLanguage != Language)
{
mCurrentLanguage = Language;
// Force UI to update with the correct string for the new language
SetActiveString( mCurrentStringIndex );
}
}
void CStringEditor::SetActiveString(int StringIndex)
{
mCurrentStringIndex = StringIndex;
TString StringName = mpStringTable->StringNameByIndex(mCurrentStringIndex);
TString StringData = mpStringTable->GetString(mCurrentLanguage, mCurrentStringIndex);
mpUI->StringNameLineEdit->setText( TO_QSTRING(StringName) );
mpUI->StringTextEdit->setPlainText( TO_QSTRING(StringData) );
}
void CStringEditor::LoadSettings()
{
QSettings Settings;
mCurrentLanguage = (ELanguage) Settings.value(gkpLanguageSetting, (int) ELanguage::English).toInt();
}
void CStringEditor::SaveSettings()
{
QSettings Settings;
Settings.setValue(gkpLanguageSetting, (int) mCurrentLanguage);
}
/** Slots */
void CStringEditor::OnStringSelected(const QModelIndex& kIndex)
{
SetActiveString( kIndex.row() );
}
void CStringEditor::OnLanguageChanged(int LanguageIndex)
{
ASSERT( LanguageIndex >= 0 && LanguageIndex < (int) mpStringTable->NumLanguages() );
ELanguage Language = mpStringTable->LanguageByIndex(LanguageIndex);
SetActiveLanguage(Language);
}

View File

@@ -0,0 +1,51 @@
#ifndef CSTRINGEDITOR_H
#define CSTRINGEDITOR_H
#include "IEditor.h"
#include "CStringListModel.h"
#include <Core/Resource/StringTable/CStringTable.h>
#include <QMainWindow>
namespace Ui {
class CStringEditor;
}
/** Editor window for string tables (STRG assets) */
class CStringEditor : public IEditor
{
Q_OBJECT
/** Qt UI */
Ui::CStringEditor* mpUI;
/** String table asset being edited */
TResPtr<CStringTable> mpStringTable;
/** Language being edited */
ELanguage mCurrentLanguage;
/** Index of the string being edited */
uint mCurrentStringIndex;
/** Model for the string list view */
CStringListModel* mpListModel;
public:
explicit CStringEditor(CStringTable* pStringTable, QWidget* pParent = 0);
~CStringEditor();
void InitUI();
void UpdateUI();
void SetActiveLanguage(ELanguage Language);
void SetActiveString(int StringIndex);
void LoadSettings();
void SaveSettings();
public slots:
void OnStringSelected(const QModelIndex& kIndex);
void OnLanguageChanged(int LanguageIndex);
};
#endif // CSTRINGEDITOR_H

View File

@@ -0,0 +1,209 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>CStringEditor</class>
<widget class="QMainWindow" name="CStringEditor">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>609</width>
<height>444</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<layout class="QVBoxLayout" name="verticalLayout_2" stretch="0">
<item>
<widget class="QSplitter" name="splitter">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<widget class="QGroupBox" name="StringsGroupBox">
<property name="title">
<string>Strings</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<property name="leftMargin">
<number>3</number>
</property>
<property name="topMargin">
<number>3</number>
</property>
<property name="rightMargin">
<number>3</number>
</property>
<property name="bottomMargin">
<number>3</number>
</property>
<item>
<widget class="QListView" name="StringNameListView">
<property name="font">
<font>
<pointsize>9</pointsize>
</font>
</property>
<property name="alternatingRowColors">
<bool>true</bool>
</property>
<property name="selectionMode">
<enum>QAbstractItemView::ExtendedSelection</enum>
</property>
<property name="selectionBehavior">
<enum>QAbstractItemView::SelectRows</enum>
</property>
<property name="verticalScrollMode">
<enum>QAbstractItemView::ScrollPerPixel</enum>
</property>
<property name="horizontalScrollMode">
<enum>QAbstractItemView::ScrollPerPixel</enum>
</property>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QLabel" name="NameLabel">
<property name="text">
<string>Name:</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="StringNameLineEdit">
<property name="text">
<string/>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="AddStringButton">
<property name="text">
<string/>
</property>
<property name="icon">
<iconset resource="../Icons.qrc">
<normaloff>:/icons/Plus.png</normaloff>:/icons/Plus.png</iconset>
</property>
<property name="iconSize">
<size>
<width>16</width>
<height>16</height>
</size>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="AddStringButton_2">
<property name="text">
<string/>
</property>
<property name="icon">
<iconset resource="../Icons.qrc">
<normaloff>:/icons/Minus v2.png</normaloff>:/icons/Minus v2.png</iconset>
</property>
<property name="iconSize">
<size>
<width>16</width>
<height>16</height>
</size>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
<widget class="QGroupBox" name="EditGroupBox">
<property name="title">
<string>Edit</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_4">
<property name="spacing">
<number>0</number>
</property>
<item>
<widget class="QWidget" name="TabsContainerWidget" native="true"/>
</item>
<item>
<widget class="QPlainTextEdit" name="StringTextEdit">
<property name="font">
<font>
<pointsize>9</pointsize>
</font>
</property>
</widget>
</item>
</layout>
</widget>
</widget>
</item>
</layout>
</widget>
<widget class="QToolBar" name="ToolBar">
<property name="windowTitle">
<string>toolBar</string>
</property>
<property name="iconSize">
<size>
<width>32</width>
<height>32</height>
</size>
</property>
<attribute name="toolBarArea">
<enum>TopToolBarArea</enum>
</attribute>
<attribute name="toolBarBreak">
<bool>false</bool>
</attribute>
<addaction name="ActionSave"/>
<addaction name="ActionSaveAndCook"/>
</widget>
<widget class="QStatusBar" name="StatusBar"/>
<action name="ActionSave">
<property name="icon">
<iconset resource="../Icons.qrc">
<normaloff>:/icons/Save.png</normaloff>:/icons/Save.png</iconset>
</property>
<property name="text">
<string>Save</string>
</property>
<property name="toolTip">
<string>Save</string>
</property>
<property name="shortcut">
<string>Ctrl+S</string>
</property>
</action>
<action name="ActionSaveAndCook">
<property name="icon">
<iconset resource="../Icons.qrc">
<normaloff>:/icons/SaveAndRepack_32px.png</normaloff>:/icons/SaveAndRepack_32px.png</iconset>
</property>
<property name="text">
<string>Save and Cook</string>
</property>
<property name="toolTip">
<string>Save and Cook</string>
</property>
</action>
</widget>
<resources>
<include location="../Icons.qrc"/>
</resources>
<connections/>
</ui>

View File

@@ -0,0 +1,69 @@
#include "CStringListModel.h"
#include "Editor/UICommon.h"
CStringListModel::CStringListModel(CStringTable* pInStrings, QObject* pParent /*= 0*/)
: QAbstractListModel(pParent)
, mpStringTable(pInStrings)
, mStringPreviewLanguage(ELanguage::English)
{
}
/** Change the preview language display */
void CStringListModel::SetPreviewLanguage(ELanguage InLanguage)
{
if (mStringPreviewLanguage != InLanguage)
{
mStringPreviewLanguage = InLanguage;
// Emit data changed for user role for the full range of strings
int NumStrings = mpStringTable ? mpStringTable->NumStrings() : 0;
if (NumStrings)
{
QVector<int> Roles;
Roles << Qt::UserRole;
emit dataChanged( index(0), index(NumStrings-1), Roles );
}
}
}
/** QAbstractListModel interface */
int CStringListModel::rowCount(const QModelIndex& kParent) const
{
return mpStringTable ? mpStringTable->NumStrings() : 0;
}
QVariant CStringListModel::data(const QModelIndex& kIndex, int Role) const
{
if (!kIndex.isValid() || !mpStringTable)
{
return QVariant::Invalid;
}
int StringIndex = kIndex.row();
// display/tooltip role: return the string name
if (Role == Qt::DisplayRole || Role == Qt::ToolTipRole)
{
TString StringName = mpStringTable->StringNameByIndex(StringIndex);
if (StringName.IsEmpty())
{
StringName = TString::Format("<String #%d>", kIndex.row()+1);
}
return TO_QSTRING(StringName);
}
// user role: used for string preview, return the string contents
else if (Role == Qt::UserRole)
{
TString StringData = mpStringTable->GetString(mStringPreviewLanguage, StringIndex);
StringData.Replace("\n", " ");
return TO_QSTRING(StringData);
}
// other roles: invalid
else
{
return QVariant::Invalid;
}
}

View File

@@ -0,0 +1,28 @@
#ifndef CSTRINGLISTMODEL_H
#define CSTRINGLISTMODEL_H
#include <QAbstractListModel>
#include <Core/Resource/TResPtr.h>
#include <Core/Resource/StringTable/CStringTable.h>
/** Model for listing available strings in a string table */
class CStringListModel : public QAbstractListModel
{
/** Asset to pull the strings from */
TResPtr<CStringTable> mpStringTable;
/** Language to use for the string preview for modes that support it */
ELanguage mStringPreviewLanguage;
public:
CStringListModel(CStringTable* pInStrings, QObject* pParent = 0);
/** Change the preview language display */
void SetPreviewLanguage(ELanguage InLanguage);
/** QAbstractListModel interface */
virtual int rowCount(const QModelIndex& kParent) const override;
virtual QVariant data(const QModelIndex& kIndex, int Role) const override;
};
#endif // CSTRINGLISTMODEL_H