diff options
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | seentl.cpp | 20 | ||||
-rw-r--r-- | seentl.h | 3 | ||||
-rw-r--r-- | utfscriptmodel.cpp | 28 | ||||
-rw-r--r-- | utfscriptmodel.h | 4 |
5 files changed, 48 insertions, 8 deletions
@@ -3,4 +3,5 @@ Makefile* debug/ qtc-gdbmacros/ seentl +seentl.pro.user ui_seentl.h @@ -1,6 +1,9 @@ #include "seentl.h" #include "ui_seentl.h" #include <QHeaderView> +#include <QFileDialog> +#include <QFileInfo> +#include <QMessageBox> #include <QDebug> SEENTL::SEENTL(QWidget *parent) @@ -8,8 +11,6 @@ SEENTL::SEENTL(QWidget *parent) { ui->setupUi(this); - script.openScript("SEEN0517.utf"); - // some settings to make the lines table look good ui->lines->verticalHeader()->setDefaultSectionSize(ui->lines->fontMetrics().height()+1); ui->lines->horizontalHeader()->setVisible(0); @@ -20,9 +21,24 @@ SEENTL::SEENTL(QWidget *parent) ui->lines->horizontalHeader()->setResizeMode(2, QHeaderView::Stretch); ui->lines->horizontalHeader()->setResizeMode(3, QHeaderView::Fixed); ui->lines->horizontalHeader()->resizeSection(3, 100); + + connect(ui->actionOpen, SIGNAL(triggered()), this, SLOT(openScript())); } SEENTL::~SEENTL() { delete ui; } + +void SEENTL::openScript() +{ + QString fn = QFileDialog::getOpenFileName(this, "Open File", "", "Script files (*.utf)"); + if(fn == "") + return; + QFileInfo fi(fn); + if(!fi.exists() || !fi.isFile() || !fi.isReadable() || !fi.isWritable()) { + QMessageBox::critical(this, "Can't open file", "File either doesn't exist or isn't readable and writable"); + return; + } + script.openScript(fn); +} @@ -17,6 +17,9 @@ public: SEENTL(QWidget *parent = 0); ~SEENTL(); +public slots: + void openScript(); + private: Ui::SEENTLClass *ui; utfScriptModel script; diff --git a/utfscriptmodel.cpp b/utfscriptmodel.cpp index d7ef114..4b75959 100644 --- a/utfscriptmodel.cpp +++ b/utfscriptmodel.cpp @@ -17,24 +17,44 @@ utfScriptModel::utfScriptModel(const QString &file) utfScriptModel::~utfScriptModel() { - fl->close(); + closeScript(); delete fl; - if(positions != NULL) - delete [] positions; } void utfScriptModel::openScript(const QString &file) { + if(fl->isOpen()) + closeScript(); + 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(); + + // we don't know how many rows we're going to insert after actually inserting them + beginInsertRows(QModelIndex(), 0, rows-1); + endInsertRows(); +} + +void utfScriptModel::closeScript() +{ + if(!fl->isOpen()) + return; + beginRemoveRows(QModelIndex(), 0, rows-1); + rows = 0; + lines.clear(); + if(positions != NULL) + delete [] positions; + positions = NULL; + endRemoveRows(); + fl->close(); } // updates int rows and int *positions @@ -45,7 +65,7 @@ void utfScriptModel::calcPositions() // 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]; + positions = new int[lines.size()+1]; rows = 0; int i; diff --git a/utfscriptmodel.h b/utfscriptmodel.h index c107c52..1a1ab94 100644 --- a/utfscriptmodel.h +++ b/utfscriptmodel.h @@ -12,6 +12,7 @@ public: utfScriptModel(const QString&); ~utfScriptModel(); void openScript(const QString&); + void closeScript(); int rowCount(const QModelIndex &parent = QModelIndex()) const; int columnCount(const QModelIndex &parent = QModelIndex()) const; QVariant data(const QModelIndex &index, int role) const; @@ -21,8 +22,7 @@ public: private: QFile *fl; QStringList lines; - int rows; - qint64 *positions; + int rows, *positions; void calcPositions(); }; |