Skip to content
This repository has been archived by the owner on Nov 1, 2017. It is now read-only.

Commit

Permalink
Improve clean database start.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike McQuaid committed Apr 29, 2011
1 parent 17aa9d5 commit 2b5fd81
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ bool Database::create()
if (!sqlQuery.exec(tableQuery)) {
qWarning() << tableQuery;
qWarning() << tr("Unable to create '%1' table:").arg(name);
qWarning() << sqlQuery.lastError();
qWarning() << sqlQuery.lastError().text();
return false;
}
}
Expand Down
24 changes: 15 additions & 9 deletions MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow),
database(0),
preferences(new PreferencesDialog()),
eventsFilterModel(0),
conversationsTreeModel(0)
{
Expand All @@ -64,8 +65,6 @@ MainWindow::MainWindow(QWidget *parent) :
ui->splitter->setSizes(splitterSizes);
}

PreferencesDialog *preferences = new PreferencesDialog(this);

connect(ui->actionNew, SIGNAL(triggered()), this, SLOT(newFile()));
connect(ui->actionOpen, SIGNAL(triggered()), this, SLOT(openFile()));
connect(ui->actionAdd_Event, SIGNAL(triggered()), this, SLOT(addEvent()));
Expand Down Expand Up @@ -140,6 +139,8 @@ void MainWindow::openFile(QString fileName)
reloadEvents();
setWindowTitle(QString("%1 - Fabula").arg(fileName));
ui->centralWidget->setEnabled(true);
if (!preferences->haveWriter())
preferences->open();
}
else {
if (!database) {
Expand Down Expand Up @@ -247,11 +248,13 @@ void MainWindow::addOrEditTreeItem(DialogMode mode, TreeItem treeItem, const QMo
QModelIndexList itemIndexes =
tableModel->match(firstItem, Qt::DisplayRole, conversation);

Q_ASSERT(!itemIndexes.isEmpty());
if (itemIndexes.isEmpty())
return;
//Q_ASSERT(!itemIndexes.isEmpty());
//if (itemIndexes.isEmpty())
// return;

QModelIndex itemIndex = itemIndexes.first();
QModelIndex itemIndex;
if (!itemIndexes.isEmpty())
itemIndex = itemIndexes.first();

editViewItem(itemIndex, tableDialog, mode);
}
Expand Down Expand Up @@ -283,10 +286,12 @@ void MainWindow::deleteCharacter()
void MainWindow::editViewItem(const QModelIndex &index, SqlRelationalTableDialog *dialog,
DialogMode mode)
{
const QModelIndex &rootIndex = rootModelIndex(index);
Q_ASSERT(rootIndex.isValid());
int row = 0;

const int row = rootIndex.row();
const QModelIndex &rootIndex = rootModelIndex(index);
//Q_ASSERT(rootIndex.isValid());
if (rootIndex.isValid())
row = rootIndex.row();

Q_ASSERT(dialog);
if (!dialog)
Expand Down Expand Up @@ -371,6 +376,7 @@ MainWindow::~MainWindow()
{
settings.setValue("treeSize", ui->splitter->sizes().first());
settings.setValue("tableSize", ui->splitter->sizes().last());
delete preferences;
delete ui;
delete database;
}
Expand Down
3 changes: 3 additions & 0 deletions MainWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ namespace Ui {
}

class Database;
class PreferencesDialog;
class QAbstractItemView;
class QSqlRelationalTableModel;
class QSortFilterProxyModel;
Expand Down Expand Up @@ -82,6 +83,8 @@ public slots:
private:
Ui::MainWindow *ui;
Database *database;
PreferencesDialog *preferences;

QSettings settings;
QDesktopServices desktopServices;
QSortFilterProxyModel *eventsFilterModel;
Expand Down
29 changes: 24 additions & 5 deletions PreferencesDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,21 @@ PreferencesDialog::PreferencesDialog(QWidget *parent) :
ui->setupUi(this);
QSqlTableModel *writersModel = new QSqlTableModel(this);
writersModel->setTable(Database::tableName(Database::Writer));
writersModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
//writersModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
writersModel->select();
ui->writerComboBox->setModel(writersModel);
ui->writerComboBox->setModelColumn(1);

setWindowModality(Qt::WindowModal);

load();

connect(this, SIGNAL(accepted()), this, SLOT(save()));
}

PreferencesDialog::~PreferencesDialog()
{
save();
delete ui;
}

Expand All @@ -35,10 +38,19 @@ void PreferencesDialog::open()
QDialog::open();
}

void PreferencesDialog::close()
{
save();
QDialog::close();
}

void PreferencesDialog::load()
{
QSettings settings;
QString writer = settings.value("writer").toString();
const QVariant writerVariant = QSettings().value("writer");
ui->writerNameLabel->setVisible(!writerVariant.isValid());
if (!writerVariant.isValid())
return;
const QString& writer = writerVariant.toString();
int writerIndex = ui->writerComboBox->findText(writer);
ui->writerComboBox->setCurrentIndex(writerIndex);
}
Expand All @@ -52,8 +64,15 @@ void PreferencesDialog::save()
return;

const QString &writer = ui->writerComboBox->currentText();
if (writer.isEmpty())
return;

writersModel->submitAll();
QSettings().setValue("writer", writer);
}

QSettings settings;
settings.setValue("writer", writer);
bool PreferencesDialog::haveWriter()
{
return true;
//return QSettings().value("writer").isValid();
}
2 changes: 2 additions & 0 deletions PreferencesDialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ class PreferencesDialog : public QDialog

public slots:
void open();
void close();
void load();
void save();
bool haveWriter();

private:
Ui::PreferencesDialog *ui;
Expand Down
11 changes: 9 additions & 2 deletions PreferencesDialog.ui
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,21 @@
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
<width>187</width>
<height>110</height>
</rect>
</property>
<property name="windowTitle">
<string>Dialog</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QLabel" name="writerNameLabel">
<property name="text">
<string>Please enter your name:</string>
</property>
</widget>
</item>
<item>
<layout class="QFormLayout" name="formLayout">
<property name="fieldGrowthPolicy">
Expand Down

0 comments on commit 2b5fd81

Please sign in to comment.