From 0c91be8eacd49a570cb2304228a66a61f0d31bdd Mon Sep 17 00:00:00 2001 From: Felix Geyer Date: Wed, 6 Oct 2010 19:40:50 +0200 Subject: [PATCH] Add initial UI for entry editing. --- src/CMakeLists.txt | 4 ++ src/gui/DatabaseWidget.cpp | 34 ++++++++++--- src/gui/DatabaseWidget.h | 12 ++++- src/gui/EditEntryWidget.cpp | 87 +++++++++++++++++++++++++++++++++ src/gui/EditEntryWidget.h | 63 ++++++++++++++++++++++++ src/gui/EditEntryWidget.ui | 52 ++++++++++++++++++++ src/gui/EditEntryWidgetMain.ui | 51 +++++++++++++++++++ src/gui/EditEntryWidgetNotes.ui | 28 +++++++++++ src/gui/EntryView.cpp | 2 + src/gui/GroupView.cpp | 11 ++++- 10 files changed, 334 insertions(+), 10 deletions(-) create mode 100644 src/gui/EditEntryWidget.cpp create mode 100644 src/gui/EditEntryWidget.h create mode 100644 src/gui/EditEntryWidget.ui create mode 100644 src/gui/EditEntryWidgetMain.ui create mode 100644 src/gui/EditEntryWidgetNotes.ui diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index aa8fc04c..6f5defb8 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -35,6 +35,7 @@ set(keepassx_SOURCES format/KeePass2XmlReader.cpp format/KeePass2XmlWriter.cpp gui/DatabaseWidget.cpp + gui/EditEntryWidget.cpp gui/EntryModel.cpp gui/EntryView.cpp gui/GroupModel.cpp @@ -49,6 +50,9 @@ set(keepassx_SOURCES ) set(keepassx_FORMS + gui/EditEntryWidget.ui + gui/EditEntryWidgetMain.ui + gui/EditEntryWidgetNotes.ui gui/MainWindow.ui ) diff --git a/src/gui/DatabaseWidget.cpp b/src/gui/DatabaseWidget.cpp index 1a4d1645..d1d2a81e 100644 --- a/src/gui/DatabaseWidget.cpp +++ b/src/gui/DatabaseWidget.cpp @@ -20,14 +20,16 @@ #include #include +#include "EditEntryWidget.h" #include "EntryView.h" #include "GroupView.h" DatabaseWidget::DatabaseWidget(Database* db, QWidget* parent) - : QWidget(parent) + : QStackedWidget(parent) { - QLayout* layout = new QHBoxLayout(this); - QSplitter* splitter = new QSplitter(this); + m_mainWidget = new QWidget(this); + QLayout* layout = new QHBoxLayout(m_mainWidget); + QSplitter* splitter = new QSplitter(m_mainWidget); m_groupView = new GroupView(db, splitter); m_entryView = new EntryView(splitter); @@ -42,11 +44,31 @@ DatabaseWidget::DatabaseWidget(Database* db, QWidget* parent) policy.setHorizontalStretch(70); m_entryView->setSizePolicy(policy); - connect(m_groupView, SIGNAL(groupChanged(Group*)), m_entryView, SLOT(setGroup(Group*))); - splitter->addWidget(m_groupView); splitter->addWidget(m_entryView); layout->addWidget(splitter); - setLayout(layout); + m_mainWidget->setLayout(layout); + + m_editWidget = new EditEntryWidget(); + + addWidget(m_mainWidget); + addWidget(m_editWidget); + + connect(m_groupView, SIGNAL(groupChanged(Group*)), m_entryView, SLOT(setGroup(Group*))); + connect(m_entryView, SIGNAL(entryActivated(Entry*)), SLOT(switchToEdit(Entry*))); + connect(m_editWidget, SIGNAL(editFinished()), SLOT(switchToView())); + + setCurrentIndex(0); +} + +void DatabaseWidget::switchToView() +{ + setCurrentIndex(0); +} + +void DatabaseWidget::switchToEdit(Entry* entry) +{ + m_editWidget->loadEntry(entry); + setCurrentIndex(1); } diff --git a/src/gui/DatabaseWidget.h b/src/gui/DatabaseWidget.h index 2aa76a49..e9529989 100644 --- a/src/gui/DatabaseWidget.h +++ b/src/gui/DatabaseWidget.h @@ -18,20 +18,28 @@ #ifndef KEEPASSX_DATABASEWIDGET_H #define KEEPASSX_DATABASEWIDGET_H -#include +#include class Database; +class EditEntryWidget; +class Entry; class EntryView; class GroupView; -class DatabaseWidget : public QWidget +class DatabaseWidget : public QStackedWidget { Q_OBJECT public: explicit DatabaseWidget(Database* db, QWidget* parent = 0); +private Q_SLOTS: + void switchToView(); + void switchToEdit(Entry* entry); + private: + QWidget* m_mainWidget; + EditEntryWidget* m_editWidget; GroupView* m_groupView; EntryView* m_entryView; }; diff --git a/src/gui/EditEntryWidget.cpp b/src/gui/EditEntryWidget.cpp new file mode 100644 index 00000000..ff2a4e52 --- /dev/null +++ b/src/gui/EditEntryWidget.cpp @@ -0,0 +1,87 @@ +/* + * Copyright (C) 2010 Felix Geyer + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 or (at your option) + * version 3 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "EditEntryWidget.h" +#include "ui_EditEntryWidget.h" +#include "ui_EditEntryWidgetMain.h" +#include "ui_EditEntryWidgetNotes.h" + +#include +#include + +#include "core/Entry.h" + +EditEntryWidget::EditEntryWidget(QWidget* parent) + : QWidget(parent) + , m_entry(0) + , m_ui(new Ui::EditEntryWidget()) + , m_mainUi(new Ui::EditEntryWidgetMain()) + , m_notesUi(new Ui::EditEntryWidgetNotes()) + , m_mainWidget(new QWidget(this)) + , m_notesWidget(new QWidget(this)) +{ + m_ui->setupUi(this); + + m_ui->categoryList->addItem(tr("Entry")); + m_ui->categoryList->addItem(tr("Description")); + + m_mainUi->setupUi(m_mainWidget); + m_ui->stackedWidget->addWidget(m_mainWidget); + + m_notesUi->setupUi(m_notesWidget); + m_ui->stackedWidget->addWidget(m_notesWidget); + + Q_ASSERT(m_ui->categoryList->model()->rowCount() == m_ui->stackedWidget->count()); + + connect(m_ui->categoryList, SIGNAL(currentRowChanged(int)), m_ui->stackedWidget, SLOT(setCurrentIndex(int))); + connect(m_ui->buttonBox, SIGNAL(accepted()), SLOT(saveEntry())); + connect(m_ui->buttonBox, SIGNAL(rejected()), SLOT(cancel())); +} + +EditEntryWidget::~EditEntryWidget() +{ +} + +void EditEntryWidget::loadEntry(Entry* entry) +{ + m_entry = entry; + + m_mainUi->titleEdit->setText(entry->title()); + m_mainUi->usernameEdit->setText(entry->username()); + m_mainUi->urlEdit->setText(entry->url()); + + m_notesUi->notesEdit->setPlainText(entry->notes()); + + m_ui->categoryList->setCurrentRow(0); +} + +void EditEntryWidget::saveEntry() +{ + m_entry->setTitle(m_mainUi->titleEdit->text()); + m_entry->setUsername(m_mainUi->usernameEdit->text()); + m_entry->setUrl(m_mainUi->urlEdit->text()); + + m_entry->setNotes(m_notesUi->notesEdit->toPlainText()); + + cancel(); +} + +void EditEntryWidget::cancel() +{ + m_entry = 0; + Q_EMIT editFinished(); +} diff --git a/src/gui/EditEntryWidget.h b/src/gui/EditEntryWidget.h new file mode 100644 index 00000000..e760bfa8 --- /dev/null +++ b/src/gui/EditEntryWidget.h @@ -0,0 +1,63 @@ +/* + * Copyright (C) 2010 Felix Geyer + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 or (at your option) + * version 3 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef KEEPASSX_EDITENTRYWIDGET_H +#define KEEPASSX_EDITENTRYWIDGET_H + +#include +#include + +class Entry; +class QListWidget; +class QStackedLayout; + +namespace Ui { + class EditEntryWidget; + class EditEntryWidgetMain; + class EditEntryWidgetNotes; +} + +class EditEntryWidget : public QWidget +{ + Q_OBJECT + +public: + explicit EditEntryWidget(QWidget* parent = 0); + ~EditEntryWidget(); + + void loadEntry(Entry* entry); + +Q_SIGNALS: + void editFinished(); + +private Q_SLOTS: + void saveEntry(); + void cancel(); + +private: + Entry* m_entry; + + QScopedPointer m_ui; + QScopedPointer m_mainUi; + QScopedPointer m_notesUi; + QWidget* m_mainWidget; + QWidget* m_notesWidget; + + Q_DISABLE_COPY(EditEntryWidget) +}; + +#endif // KEEPASSX_EDITENTRYWIDGET_H diff --git a/src/gui/EditEntryWidget.ui b/src/gui/EditEntryWidget.ui new file mode 100644 index 00000000..047bfa49 --- /dev/null +++ b/src/gui/EditEntryWidget.ui @@ -0,0 +1,52 @@ + + + EditEntryWidget + + + + 0 + 0 + 400 + 300 + + + + + + + + + + 20 + 0 + + + + + + + + + 80 + 0 + + + + -1 + + + + + + + + + QDialogButtonBox::Cancel|QDialogButtonBox::Ok + + + + + + + + diff --git a/src/gui/EditEntryWidgetMain.ui b/src/gui/EditEntryWidgetMain.ui new file mode 100644 index 00000000..26ec5396 --- /dev/null +++ b/src/gui/EditEntryWidgetMain.ui @@ -0,0 +1,51 @@ + + + EditEntryWidgetMain + + + + 0 + 0 + 372 + 301 + + + + Form + + + + + + Title: + + + + + + + + + + Username: + + + + + + + + + + + + + URL: + + + + + + + + diff --git a/src/gui/EditEntryWidgetNotes.ui b/src/gui/EditEntryWidgetNotes.ui new file mode 100644 index 00000000..d2e7ab94 --- /dev/null +++ b/src/gui/EditEntryWidgetNotes.ui @@ -0,0 +1,28 @@ + + + EditEntryWidgetNotes + + + + 0 + 0 + 400 + 300 + + + + + + + Notes: + + + + + + + + + + + diff --git a/src/gui/EntryView.cpp b/src/gui/EntryView.cpp index ac3130f2..b041c184 100644 --- a/src/gui/EntryView.cpp +++ b/src/gui/EntryView.cpp @@ -27,6 +27,8 @@ EntryView::EntryView(QWidget* parent) setUniformRowHeights(true); setRootIsDecorated(false); + + connect(this, SIGNAL(activated(const QModelIndex&)), SLOT(emitEntryActivated(const QModelIndex&))); } void EntryView::setGroup(Group* group) diff --git a/src/gui/GroupView.cpp b/src/gui/GroupView.cpp index 617c3adc..46a26910 100644 --- a/src/gui/GroupView.cpp +++ b/src/gui/GroupView.cpp @@ -17,6 +17,8 @@ #include "GroupView.h" +#include + #include "core/Database.h" #include "core/Group.h" #include "gui/GroupModel.h" @@ -26,12 +28,17 @@ GroupView::GroupView(Database* db, QWidget* parent) { m_model = new GroupModel(db, this); QTreeView::setModel(m_model); - recInitExpanded(db->rootGroup()); setHeaderHidden(true); + setUniformRowHeights(true); connect(this, SIGNAL(clicked(const QModelIndex&)), SLOT(emitGroupChanged(const QModelIndex&))); - setUniformRowHeights(true); + recInitExpanded(db->rootGroup()); + + setCurrentIndex(m_model->index(0, 0)); + // invoke later so the EntryView is connected + QMetaObject::invokeMethod(this, "emitGroupChanged", Qt::QueuedConnection, + Q_ARG(QModelIndex, m_model->index(0, 0))); } void GroupView::expandedChanged(const QModelIndex& index)