From 385ed9346cd87eeb2645cf0e217d59718844293f Mon Sep 17 00:00:00 2001 From: Yorhel Date: Mon, 2 Feb 2009 19:34:03 +0100 Subject: Started on utfScriptModel and using a QTableView to display the lines It works, kind of... I have no clue as to what the hell I am doing, as I'm really just playing around. C++ isn't my thing... Don't be surprised if everything I've written just now will be completely rewritten in a next commit. --- .gitignore | 6 +++ seentl.cpp | 5 +++ seentl.h | 3 ++ seentl.pro | 21 ++++------- seentl.ui | 68 +++++++++++++++++++++++++++------- utfscriptmodel.cpp | 105 +++++++++++++++++++++++++++++++++++++++++++++++++++++ utfscriptmodel.h | 30 +++++++++++++++ 7 files changed, 212 insertions(+), 26 deletions(-) create mode 100644 .gitignore create mode 100644 utfscriptmodel.cpp create mode 100644 utfscriptmodel.h diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..94f0664 --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +Makefile* +*.utf +debug/ +qtc-gdbmacros/ +seentl +ui_seentl.h diff --git a/seentl.cpp b/seentl.cpp index 08ddbab..c84d412 100644 --- a/seentl.cpp +++ b/seentl.cpp @@ -5,6 +5,11 @@ SEENTL::SEENTL(QWidget *parent) : QMainWindow(parent), ui(new Ui::SEENTLClass) { ui->setupUi(this); + + script.openScript("SEEN0517.utf"); + + ui->lines->setModel(&script); + } SEENTL::~SEENTL() diff --git a/seentl.h b/seentl.h index 91889fc..c082ca5 100644 --- a/seentl.h +++ b/seentl.h @@ -2,6 +2,7 @@ #define SEENTL_H #include +#include "utfscriptmodel.h" namespace Ui { @@ -18,6 +19,8 @@ public: private: Ui::SEENTLClass *ui; + utfScriptModel script; + }; #endif // SEENTL_H diff --git a/seentl.pro b/seentl.pro index 79b08d5..f1482c9 100644 --- a/seentl.pro +++ b/seentl.pro @@ -1,16 +1,11 @@ -#------------------------------------------------- -# +# ------------------------------------------------- # Project created by QtCreator 2009-02-02T14:14:07 -# -#------------------------------------------------- - +# ------------------------------------------------- TARGET = seentl TEMPLATE = app - - -SOURCES += main.cpp\ - seentl.cpp - -HEADERS += seentl.h - -FORMS += seentl.ui +SOURCES += main.cpp \ + seentl.cpp \ + utfscriptmodel.cpp +HEADERS += seentl.h \ + utfscriptmodel.h +FORMS += seentl.ui diff --git a/seentl.ui b/seentl.ui index fae4998..536e094 100644 --- a/seentl.ui +++ b/seentl.ui @@ -1,10 +1,8 @@ - + + SEENTLClass - - - SEENTLClass - - + + 0 0 @@ -12,16 +10,60 @@ 400 - + SEENTL - - - - + + + + + + QAbstractItemView::SingleSelection + + + QAbstractItemView::SelectRows + + + + + + + + + 0 + 0 + 600 + 23 + + + + + &File + + + + + + + + + + Open + + + Ctrl+O + + + + + Save + + + Ctrl+S + + - - + diff --git a/utfscriptmodel.cpp b/utfscriptmodel.cpp new file mode 100644 index 0000000..d7ef114 --- /dev/null +++ b/utfscriptmodel.cpp @@ -0,0 +1,105 @@ +#include +#include +#include "utfscriptmodel.h" + +utfScriptModel::utfScriptModel() +{ + fl = new QFile(this); + rows = 0; + positions = NULL; +} + +utfScriptModel::utfScriptModel(const QString &file) +{ + utfScriptModel(); + openScript(file); +} + +utfScriptModel::~utfScriptModel() +{ + fl->close(); + delete fl; + if(positions != NULL) + delete [] positions; +} + +void utfScriptModel::openScript(const QString &file) +{ + QTextStream str; + fl->setFileName(file); + fl->open(QIODevice::ReadWrite | QIODevice::Text); + str.setDevice(fl); + str.setCodec("UTF-8"); + str.seek(0); + // let's hog some memory, baby! + while(!str.atEnd()) + lines.append(str.readLine()); + calcPositions(); +} + +// updates int rows and int *positions +void utfScriptModel::calcPositions() +{ + if(positions != NULL) + delete [] positions; + + // lines.size() is actually way too much, but at least we can be + // sure it's enough without having to calculate the actual size + positions = new qint64[lines.size()+1]; + + rows = 0; + int i; + for(i=0; i"))) + positions[rows++] = i; + positions[rows] = i; +} + +int utfScriptModel::rowCount(const QModelIndex &parent) const +{ + if(!fl->isOpen()) + return 0; + return rows; +} + +int utfScriptModel::columnCount(const QModelIndex &parent) const +{ + return 4; // #line, original, translation, comments +} + +QVariant utfScriptModel::data(const QModelIndex &index, int role) const +{ + if(!index.isValid() || index.row() >= rows || index.column() > 3 || role != Qt::DisplayRole) + return QVariant(); + + QRegExp tra("<(\\d{4})>\\s*(.+)"); + QRegExp ori("//\\s*<(\\d{4})>\\s*(.+)"); + QRegExp com("//(.+)"); + + QString ln, num, orig, trans, comm; + for(int i = positions[index.row()]; i < positions[index.row()+1]; i++) { + ln = lines.at(i); + if(tra.exactMatch(ln)) { + num = tra.cap(1); + trans = tra.cap(2); + } else if(ori.exactMatch(ln)) { + orig = ori.cap(2); + if(orig == trans) + trans = ""; + } else if(com.exactMatch(ln)) { + if(comm != "") + comm += "\n"; + comm += com.cap(1); + } else + break; + } + + return index.column() == 0 ? num : index.column() == 1 ? orig : index.column() == 2 ? trans : comm; +} + +QVariant utfScriptModel::headerData(int section, Qt::Orientation orientation, int role) const +{ + if(orientation == Qt::Vertical || role != Qt::DisplayRole) + return QVariant(); + return QString(section == 0 ? "Line" : section == 1 ? "Original" : section == 2 ? "Translation" : "Comments"); +} diff --git a/utfscriptmodel.h b/utfscriptmodel.h new file mode 100644 index 0000000..c107c52 --- /dev/null +++ b/utfscriptmodel.h @@ -0,0 +1,30 @@ +#ifndef UTFSCRIPTMODEL_H +#define UTFSCRIPTMODEL_H + +#include +#include +#include + +class utfScriptModel : public QAbstractTableModel +{ +public: + utfScriptModel(); + utfScriptModel(const QString&); + ~utfScriptModel(); + void openScript(const QString&); + int rowCount(const QModelIndex &parent = QModelIndex()) const; + int columnCount(const QModelIndex &parent = QModelIndex()) const; + QVariant data(const QModelIndex &index, int role) const; + QVariant headerData(int section, Qt::Orientation orientation, + int role = Qt::DisplayRole) const; + +private: + QFile *fl; + QStringList lines; + int rows; + qint64 *positions; + + void calcPositions(); +}; + +#endif // UTFSCRIPTMODEL_H -- cgit v1.2.3