Skip to content

Commit

Permalink
add support for building with Qt 6
Browse files Browse the repository at this point in the history
  • Loading branch information
kambala-decapitator committed Dec 17, 2023
1 parent a457953 commit dc809a9
Show file tree
Hide file tree
Showing 15 changed files with 171 additions and 54 deletions.
6 changes: 6 additions & 0 deletions QTblEditor.pro
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ greaterThan(QT_MAJOR_VERSION, 4): {
QT += widgets
*-clang*: cache()
}
greaterThan(QT_MAJOR_VERSION, 5) {
DEFINES += IS_QT6
QT += core5compat
}

CONFIG(release, debug|release): {
IS_RELEASE_BUILD = 1
Expand All @@ -24,6 +28,7 @@ CONFIG(release, debug|release): {
# Input
HEADERS += editstringcell.h \
qtbleditor.h \
qtcompat.h \
tblstructure.h \
editstringcelldialog.h \
findreplacedialog.h \
Expand All @@ -46,6 +51,7 @@ FORMS += editstringcell.ui \
SOURCES += editstringcell.cpp \
main.cpp \
qtbleditor.cpp \
qtcompat.cpp \
tblstructure.cpp \
editstringcelldialog.cpp \
findreplacedialog.cpp \
Expand Down
63 changes: 34 additions & 29 deletions colors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,46 @@ int colorHeaderSize = 2; // const
int colorsNum = 13; // const

#include <QColor>
#include <QLatin1String>

// first bytes of color code: symbols in Unicode - U+00FF, U+0063
const QChar colorBytes[] = {0x00FF, 0x0063};
const QChar colorBytes[] = {ushort(0x00FF), ushort(0x0063)};
QString colorHeader(colorBytes, colorHeaderSize); // string representation of colorBytes; const

// last byte in the color code. these are default Blizzard's colors
QList<QChar> colorCodes = QList<QChar>()
<< 0x002F
<< 0x0030
<< 0x0031
<< 0x0032
<< 0x0033
<< 0x0034
<< 0x0035
<< 0x0036
<< 0x0037
<< 0x0038
<< 0x0039
<< 0x003A
<< 0x003B;
<< ushort(0x002F)
<< ushort(0x0030)
<< ushort(0x0031)
<< ushort(0x0032)
<< ushort(0x0033)
<< ushort(0x0034)
<< ushort(0x0035)
<< ushort(0x0036)
<< ushort(0x0037)
<< ushort(0x0038)
<< ushort(0x0039)
<< ushort(0x003A)
<< ushort(0x003B)
;

// user-readable color codes: colorHeader + colorCodes.at(i) == colorStrings.at(i+1)
QStringList colorStrings = QStringList()
<< "\\color;" // if there's only colorHeader
<< "\\white;"
<< "\\grey;"
<< "\\red;"
<< "\\green;"
<< "\\blue;"
<< "\\gold;"
<< "\\dgrey;"
<< "\\black;"
<< "\\tan;"
<< "\\orange;"
<< "\\yellow;"
<< "\\dgreen;"
<< "\\purple;";
<< QLatin1String("\\color;") // if there's only colorHeader
<< QLatin1String("\\white;")
<< QLatin1String("\\grey;")
<< QLatin1String("\\red;")
<< QLatin1String("\\green;")
<< QLatin1String("\\blue;")
<< QLatin1String("\\gold;")
<< QLatin1String("\\dgrey;")
<< QLatin1String("\\black;")
<< QLatin1String("\\tan;")
<< QLatin1String("\\orange;")
<< QLatin1String("\\yellow;")
<< QLatin1String("\\dgreen;")
<< QLatin1String("\\purple;")
;

// real colors
QList<QColor> colors = QList<QColor>()
Expand All @@ -53,4 +57,5 @@ QList<QColor> colors = QList<QColor>()
<< QColor(250, 170, 35)
<< QColor(Qt::yellow)
<< QColor(Qt::darkGreen)
<< QColor(150, 90, 250);
<< QColor(150, 90, 250)
;
4 changes: 2 additions & 2 deletions d2stringtablewidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ void D2StringTableWidget::deleteItems(bool isClear)

int row = range.topRow() - rowShift;
removeRow(row);
_editedItems.remove(qMakePair<int, int>(row, 0));
_editedItems.remove(qMakePair<int, int>(row, 1));
_editedItems.remove(std::pair<int, int>(row, 0));
_editedItems.remove(std::pair<int, int>(row, 1));
}
rowShift += range.rowCount();
emit currentCellChanged(currentRow(), 0, 0, 0);
Expand Down
5 changes: 3 additions & 2 deletions d2stringtablewidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <QTableWidget>
#include <QHeaderView>

#include <utility>

class QKeyEvent;
class QMouseEvent;
Expand All @@ -20,7 +21,7 @@ class D2StringTableWidget : public QTableWidget

void deleteItems(bool isClear);
void createRowAt(int row);
void addEditedItem(QTableWidgetItem *editedItem) { _editedItems[qMakePair<int, int>(editedItem->row(), editedItem->column())] = editedItem; }
void addEditedItem(QTableWidgetItem *editedItem) { _editedItems[std::pair<int, int>(editedItem->row(), editedItem->column())] = editedItem; }
void clearBackground();
void createNewEntry(int row, const QString &key, const QString &val);
void clearContents() { QTableWidget::clearContents(); _editedItems.clear(); }
Expand All @@ -44,7 +45,7 @@ public slots:
void dropEvent(QDropEvent *event);

private:
QMap<QPair<int, int>, QTableWidgetItem *> _editedItems;
QMap<std::pair<int, int>, QTableWidgetItem *> _editedItems;
bool _displayRowHex, _addToRowValue;

void editInPlace() { editItem(currentItem()); };
Expand Down
5 changes: 3 additions & 2 deletions editcolorsdialog.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "editcolorsdialog.h"
#include "qtcompat.h"

#include <QColorDialog>

Expand All @@ -9,7 +10,7 @@ EditColorsDialog::EditColorsDialog(QWidget *parent, const QStringList &colorStri
: QDialog(parent)
{
ui.setupUi(this);
ui.hexCodeLineEdit->setValidator(new QRegExpValidator(QRegExp("[\\da-fA-F]+"), ui.hexCodeLineEdit));
qtcompat::setRegexValidator(QLatin1String("[\\da-fA-F]+"), ui.hexCodeLineEdit);

for (int i = colorsNum; i < colors.size(); i++)
{
Expand Down Expand Up @@ -139,7 +140,7 @@ void EditColorsDialog::changeRgbNumbersDisplay(bool isDec)
if (isDec)
rgbLineEdit->setValidator(new QIntValidator(0, 255, rgbLineEdit));
else
rgbLineEdit->setValidator(new QRegExpValidator(QRegExp("[\\da-fA-F]{,2}"), rgbLineEdit));
qtcompat::setRegexValidator(QLatin1String("[\\da-fA-F]{,2}"), rgbLineEdit);
}
}

Expand Down
39 changes: 35 additions & 4 deletions editstringcell.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
#include "editstringcell.h"
#include "tblstructure.h"
#include "editcolorsdialog.h"
#include "qtcompat.h"

#include <QMenu>

#include <QStack>
#include <QQueue>
#include <QTextCodec>

#include <utility>


// convenience function
QString colorHexString(const QColor &c)
Expand All @@ -26,7 +29,7 @@ QString colorsRegexPattern()
{
QStringList realColorStrings = colorStrings.mid(1);
for (int i = 0; i < realColorStrings.size(); ++i)
realColorStrings[i] = QRegExp::escape(realColorStrings[i]);
realColorStrings[i] = qtcompat::regexEscape(realColorStrings[i]);
return QString("(?:%1)").arg(realColorStrings.join(QChar('|')));
}

Expand Down Expand Up @@ -116,13 +119,27 @@ void EditStringCell::setPreviewText()

// stupid HTML
QString text = ui.stringEdit->toPlainText().replace('<', "&lt;").replace('>', "&gt;");

const QString nonBlankSpace("&nbsp;");
#if IS_QT5
QRegularExpression emptyRe(" {2,}");
do
{
QRegularExpressionMatch match = emptyRe.match(text);
if (!match.hasMatch())
break;
int matchedLength = match.capturedLength();
text.replace(match.capturedStart(), matchedLength, nonBlankSpace.repeated(matchedLength));
} while (true);
#else
int emptyMatchIndex;
QRegExp emptyRe(" {2,}");
while ((emptyMatchIndex = emptyRe.indexIn(text)) != -1)
{
int matchedLength = emptyRe.matchedLength();
text.replace(emptyMatchIndex, matchedLength, QString("&nbsp;").repeated(matchedLength));
text.replace(emptyMatchIndex, matchedLength, nonBlankSpace.repeated(matchedLength));
}
#endif

bool shouldCenterAlign = ui.centerPreviewTextCheckBox->isChecked();
const QString defaultColor = colorStrings.at(1); // text is white by default
Expand All @@ -131,11 +148,15 @@ void EditStringCell::setPreviewText()
// text and colors flow top-to-bottom, but D2 renders text bottom-to-top
// HTML renders top-to-bottom

#if IS_QT5
static QRegularExpression colorRegex(colorsRegexPattern()); // one of the colors
#else
static QRegExp colorRegex(colorsRegexPattern()); // one of the colors
#endif
QString currentGlobalColor = defaultColor;
QStringList lines = text.split('\n');

typedef QPair<QString, QString> ColoredText; // first - text, second - color
typedef std::pair<QString, QString> ColoredText; // first - text, second - color
typedef QQueue<ColoredText> LineColoredQueue;

QStack<LineColoredQueue> lineStack;
Expand All @@ -147,18 +168,28 @@ void EditStringCell::setPreviewText()
int nextColorStart = 0, previousColorEnd = 0;
do
{
#if IS_QT5
QRegularExpressionMatch match = colorRegex.match(line, nextColorStart);
nextColorStart = match.capturedStart();
#else
nextColorStart = colorRegex.indexIn(line, nextColorStart);
#endif

int textLength = nextColorStart;
if (textLength != -1)
textLength -= previousColorEnd;
lineQueue.enqueue(qMakePair(line.mid(previousColorEnd, textLength), currentGlobalColor));
lineQueue.enqueue(ColoredText(line.mid(previousColorEnd, textLength), currentGlobalColor));

if (nextColorStart == -1)
break;

#if IS_QT5
currentGlobalColor = match.captured();
nextColorStart += match.capturedLength();
#else
currentGlobalColor = colorRegex.cap();
nextColorStart += colorRegex.matchedLength();
#endif
previousColorEnd = nextColorStart;
} while (true);

Expand Down
4 changes: 3 additions & 1 deletion editstringcell.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@

#include <QTableWidgetItem>

#include <utility>


QString colorHexString(const QColor &c);


typedef QPair<QTableWidgetItem *, QTableWidgetItem *> KeyValueItemsPair; // <keyItem, valueItem>
typedef std::pair<QTableWidgetItem *, QTableWidgetItem *> KeyValueItemsPair; // <keyItem, valueItem>
static const KeyValueItemsPair kEmptyKeyValuePair(0, 0);

class EditStringCell : public QWidget
Expand Down
5 changes: 2 additions & 3 deletions findreplacedialog.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "findreplacedialog.h"
#include "tablepanelwidget.h"

#include <QMessageBox>
#include <QDialog>
Expand All @@ -9,7 +8,7 @@
#include <QSettings>


FindReplaceDialog::FindReplaceDialog(QWidget *parent) : QDialog(parent)
FindReplaceDialog::FindReplaceDialog(QWidget *parent) : QDialog(parent), _currentStringIterator(_foundTableItems.end())
{
ui.setupUi(this);
setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
Expand Down Expand Up @@ -167,7 +166,7 @@ void FindReplaceDialog::getFoundStrings(const QList<QTableWidgetItem *> &foundIt

void FindReplaceDialog::replaceNext()
{
bool notSearched = !_currentStringIterator.i;
bool notSearched = _currentStringIterator == _foundTableItems.end();
if (notSearched)
findNext();
if (!_searchFailed)
Expand Down
4 changes: 2 additions & 2 deletions gotorowdialog.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "gotorowdialog.h"
#include "qtcompat.h"

#include <QPushButton>
#include <QRegExpValidator>

#include <QSettings>

Expand Down Expand Up @@ -30,7 +30,7 @@ GoToRowDialog::GoToRowDialog(QWidget *parent, int rowCount, bool rowsStartFromZe

void GoToRowDialog::changeMode(bool isHex)
{
ui.lineEditValue->setValidator(new QRegExpValidator(isHex ? QRegExp("[\\da-fA-F]+") : QRegExp("\\d+"), ui.lineEditValue));
qtcompat::setRegexValidator(QLatin1String(isHex ? "[\\da-fA-F]+" : "\\d+"), ui.lineEditValue);
ui.lineEditValue->setFocus();
}

Expand Down
19 changes: 14 additions & 5 deletions qtbleditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "gotorowdialog.h"
#include "tablepanelwidget.h"
#include "findreplacedialog.h"
#include "qtcompat.h"

#include <QMainWindow>
#include <QCloseEvent>
Expand All @@ -15,6 +16,7 @@
#include <QScrollBar>
#include <QClipboard>
#include <QListWidget>
#include <QActionGroup>

#include <QTextStream>
#include <QSettings>
Expand Down Expand Up @@ -491,10 +493,10 @@ bool QTblEditor::processTblFile(QFile *inputFile)
int maxKeyWidth = 0;
for (WORD i = 0; i < rowCount; i++)
{
QPair<QString, QString> currentDataStrings = tbl.dataStrings(i); // reading pair <key, value>
std::pair<QString, QString> currentDataStrings = tbl.dataStrings(i); // reading pair <key, value>
_currentTableWidget->createNewEntry(i, currentDataStrings.first, currentDataStrings.second);

int currentKeyWidth = QFontMetrics(_currentTableWidget->item(i, 0)->font()).width(currentDataStrings.first);
int currentKeyWidth = qtcompat::fontMetricsHorizontalAdvance(_currentTableWidget->item(i, 0)->font(), currentDataStrings.first);
if (maxKeyWidth < currentKeyWidth)
maxKeyWidth = currentKeyWidth;
}
Expand Down Expand Up @@ -582,7 +584,7 @@ bool QTblEditor::processTxtOrCsvFile(QFile *inputFile)
QString key = restoreNewlines(TblStructure::decodeKey(currentLine.left(separatorIndex)));
_currentTableWidget->createNewEntry(i, key, restoreNewlines(QString::fromUtf8(currentLine.mid(separatorIndex + keyValueSeparator.length()))));

int currentKeyWidth = QFontMetrics(_currentTableWidget->item(i, 0)->font()).width(key);
int currentKeyWidth = qtcompat::fontMetricsHorizontalAdvance(_currentTableWidget->item(i, 0)->font(), key);
if (maxKeyWidth < currentKeyWidth)
maxKeyWidth = currentKeyWidth;
}
Expand Down Expand Up @@ -686,7 +688,7 @@ void QTblEditor::save()
void QTblEditor::saveAs()
{
QString fileName = currentTablePanelWidget()->absoluteFileName();
#ifdef Q_OS_WIN32
#if defined(Q_OS_WIN32) && !defined(IS_QT6)
if (QSysInfo::WindowsVersion <= QSysInfo::WV_XP) // workaround for XP and earlier Windows versions
{
QFileInfo info(fileName);
Expand Down Expand Up @@ -1098,8 +1100,15 @@ void QTblEditor::writeSettings()
QTextStream out(&f);
out << tr("# You can place comments anywhere in the file\n# Format: name[tab]hex code[tab]hex RGB\n");
for (int i = colorsNum; i < colors.size(); i++)
{
out << colorStrings.at(i + 1) << '\t' << QString("0x%1").arg(colorCodes.at(i).unicode(), 0, 16)
<< '\t' << colorHexString(colors.at(i)) << endl;
<< '\t' << colorHexString(colors.at(i))
#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
<< Qt::endl;
#else
<< endl;
#endif
}
}
else
QMessageBox::critical(this, qApp->applicationName(),
Expand Down
Loading

0 comments on commit dc809a9

Please sign in to comment.