summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYorhel <git@yorhel.nl>2009-02-02 22:48:23 +0100
committerYorhel <git@yorhel.nl>2009-02-02 22:50:47 +0100
commit86465d17360beb77a36fb9730f0a574f9432ff22 (patch)
tree88384c827f899a0ee8e1bfc10abb2acac5a71b6c
parent1da74dc30f86f69cc3f946fd5d03bd3fb8654e53 (diff)
Opening scripts using Ctrl+S
-rw-r--r--.gitignore1
-rw-r--r--seentl.cpp20
-rw-r--r--seentl.h3
-rw-r--r--utfscriptmodel.cpp28
-rw-r--r--utfscriptmodel.h4
5 files changed, 48 insertions, 8 deletions
diff --git a/.gitignore b/.gitignore
index 94f0664..e6acf02 100644
--- a/.gitignore
+++ b/.gitignore
@@ -3,4 +3,5 @@ Makefile*
debug/
qtc-gdbmacros/
seentl
+seentl.pro.user
ui_seentl.h
diff --git a/seentl.cpp b/seentl.cpp
index e06b5d2..d9f808c 100644
--- a/seentl.cpp
+++ b/seentl.cpp
@@ -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);
+}
diff --git a/seentl.h b/seentl.h
index c082ca5..1bfad73 100644
--- a/seentl.h
+++ b/seentl.h
@@ -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();
};