From 7206143f863fa03a578efe7ecb95d1cfa4752824 Mon Sep 17 00:00:00 2001 From: nils Date: Mon, 17 Jan 2011 21:39:54 +0000 Subject: added quitdialog with checklist for documents to save, but there has to be some handling for the saving git-svn-id: http://cpunk.de/svn/src/gpg4usb/trunk@439 34ebc366-c3a9-4b3c-9f84-69acf7962910 --- fileencryptiondialog.cpp | 10 ++-- gpg4usb.pro | 6 ++- gpgwin.cpp | 7 ++- keymgmt.cpp | 4 +- keymgmt.h | 2 +- quitdialog.cpp | 128 +++++++++++++++++++++++++++++++++++++++++++++++ quitdialog.h | 61 ++++++++++++++++++++++ release/icons/error.png | Bin 0 -> 3080 bytes textedit.cpp | 77 ++++++++++++++++++++++------ textedit.h | 10 ++-- 10 files changed, 272 insertions(+), 33 deletions(-) create mode 100755 quitdialog.cpp create mode 100755 quitdialog.h create mode 100755 release/icons/error.png diff --git a/fileencryptiondialog.cpp b/fileencryptiondialog.cpp index 931aaa3..41c2639 100755 --- a/fileencryptiondialog.cpp +++ b/fileencryptiondialog.cpp @@ -21,16 +21,13 @@ #include #include -#include -#include #include #include -#include -#include #include #include -#include #include +#include +#include #include "fileencryptiondialog.h" @@ -76,7 +73,8 @@ FileEncryptionDialog::FileEncryptionDialog(GpgME::Context *ctx, QString iconPath mKeyList->hide(); mKeyList->setColumnWidth(2, 150); mKeyList->setColumnWidth(3, 150); - mKeyList->setChecked(&keyList); + mKeyList->setChecked(&keyList); + /* Setup Action */ radioEnc = new QRadioButton(tr("&Encrypt")); connect(radioEnc, SIGNAL(clicked()), this, SLOT(showKeyList())); diff --git a/gpg4usb.pro b/gpg4usb.pro index 25265f1..0ab3204 100644 --- a/gpg4usb.pro +++ b/gpg4usb.pro @@ -27,7 +27,8 @@ HEADERS += attachments.h \ settingsdialog.h \ attachmenttablemodel.h \ textedit.h \ - editorpage.h + editorpage.h \ + quitdialog.h SOURCES += attachments.cpp \ context.cpp \ gpgwin.cpp \ @@ -41,7 +42,8 @@ SOURCES += attachments.cpp \ settingsdialog.cpp \ attachmenttablemodel.cpp \ textedit.cpp \ - editorpage.cpp + editorpage.cpp \ + quitdialog.cpp RC_FILE = gpg4usb.rc # comment out line below for static building diff --git a/gpgwin.cpp b/gpgwin.cpp index 7a6e496..905d902 100644 --- a/gpgwin.cpp +++ b/gpgwin.cpp @@ -36,7 +36,7 @@ GpgWin::GpgWin() setCorner(Qt::BottomLeftCorner, Qt::LeftDockWidgetArea); setCorner(Qt::BottomRightCorner, Qt::RightDockWidgetArea); - edit = new TextEdit(); + edit = new TextEdit(iconPath); setCentralWidget(edit); /* the list of Keys available*/ @@ -298,12 +298,12 @@ void GpgWin::createActions() switchTabUpAct = new QAction(this); switchTabUpAct->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Tab)); connect(switchTabUpAct, SIGNAL(triggered()), edit, SLOT(switchTabUp())); - this->addAction(switchTabUpAct); + //this->addAction(switchTabUpAct); switchTabDownAct = new QAction(this); switchTabDownAct->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Shift+ Qt::Key_Tab)); connect(switchTabDownAct, SIGNAL(triggered()), edit, SLOT(switchTabDown())); - this->addAction(switchTabDownAct); + //this->addAction(switchTabDownAct); } void GpgWin::createMenus() @@ -424,7 +424,6 @@ void GpgWin::createDockWindows() void GpgWin::closeEvent(QCloseEvent *event) { - /** * ask to save changes, if there are * modified documents in any tab diff --git a/keymgmt.cpp b/keymgmt.cpp index 2cf21c0..6cb2a05 100755 --- a/keymgmt.cpp +++ b/keymgmt.cpp @@ -25,10 +25,10 @@ #include "keymgmt.h" -KeyMgmt::KeyMgmt(GpgME::Context *ctx, QString iconpath) +KeyMgmt::KeyMgmt(GpgME::Context *ctx, QString iconPath) { mCtx = ctx; - mIconPath = iconpath; + mIconPath = iconPath; /* the list of Keys available*/ mKeyList = new KeyList(mCtx, mIconPath); diff --git a/keymgmt.h b/keymgmt.h index cc5f72f..3c682f4 100755 --- a/keymgmt.h +++ b/keymgmt.h @@ -46,7 +46,7 @@ class KeyMgmt : public QMainWindow Q_OBJECT public: - KeyMgmt(GpgME::Context* ctx, QString iconpath); + KeyMgmt(GpgME::Context* ctx, QString iconPath); public slots: void importKeyFromFile(); diff --git a/quitdialog.cpp b/quitdialog.cpp new file mode 100755 index 0000000..e599f6b --- /dev/null +++ b/quitdialog.cpp @@ -0,0 +1,128 @@ +/* + * + * keymgmt.cpp + * + * Copyright 2008 gpg4usb-team + * + * 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 of the License, or + * (at your option) any later version. + * + * 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, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + */ + +#include +#include +#include "quitdialog.h" +class QMessageBox; + +QuitDialog::QuitDialog(QWidget *parent, QHash unsavedDocs, QString iconPath) + : QDialog(parent) +{ + setWindowTitle(tr("Unsaved files")); + setModal(true); + discarded =false; + + /* + * Table of unsaved documents + */ + QHashIterator i (unsavedDocs); + int row = 0; + mFileList = new QTableWidget(this); + mFileList->horizontalHeader()->hide(); + mFileList->setColumnCount(3); + mFileList->setColumnWidth(0, 20); + mFileList->setColumnHidden(2, true); + mFileList->verticalHeader()->hide(); + mFileList->setShowGrid(false); + mFileList->setEditTriggers(QAbstractItemView::NoEditTriggers); + mFileList->setFocusPolicy(Qt::NoFocus); + mFileList->horizontalHeader()->setStretchLastSection( true ); + + // fill the table + while (i.hasNext()) { + i.next(); + mFileList->setRowCount(mFileList->rowCount()+1); + + // checkbox in front of filename + QTableWidgetItem *tmp0 = new QTableWidgetItem(); + tmp0->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled ); + tmp0->setCheckState(Qt::Checked); + mFileList->setItem(row, 0, tmp0); + + // filename + QTableWidgetItem *tmp1 = new QTableWidgetItem(i.value()); + mFileList->setItem(row, 1, tmp1); + + // tab-index in hidden column + QTableWidgetItem *tmp2 = new QTableWidgetItem(QString::number(i.key())); + mFileList->setItem(row, 2, tmp2); + qDebug() << "tmp2:" << tmp2->text(); + ++row; + } + /* + * Warnbox with icon and text + */ + QPixmap *pixmap = new QPixmap(iconPath + "error.png"); + QLabel *warnicon = new QLabel(); + warnicon->setPixmap(*pixmap); + QLabel *warnlabel = new QLabel(tr("%1 files contain unsaved information.").arg(row)+"\n"+tr("Save the changes before closing?")); + QHBoxLayout *warnBoxLayout = new QHBoxLayout(); + warnBoxLayout->addWidget(warnicon); + warnBoxLayout->addWidget(warnlabel); + warnBoxLayout->setAlignment(Qt::AlignLeft); + QWidget *warnBox = new QWidget(this); + warnBox->setLayout(warnBoxLayout); + + QLabel *checkLabel = new QLabel(tr("Check the files you want to save")); + + QLabel *notelabel = new QLabel(tr("NOTE: If you don't save these files, all changes are lost.")+"\n"); + + QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Discard |QDialogButtonBox::Save | QDialogButtonBox::Cancel); + connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept())); + connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject())); + QPushButton* btnNoKey = buttonBox->button(QDialogButtonBox::Discard); + connect(btnNoKey, SIGNAL(clicked()), SLOT(myDiscard())); + QVBoxLayout *vbox = new QVBoxLayout(); + vbox->addWidget(warnBox); + vbox->addWidget(checkLabel); + vbox->addWidget(mFileList); + vbox->addWidget(notelabel); + vbox->addWidget(buttonBox); + this->setLayout(vbox); + +} + + +void QuitDialog::myDiscard() +{ + discarded =true; + reject(); +} + +bool QuitDialog::isDiscarded() +{ + return discarded; +} + +QList QuitDialog::getTabIdsToSave() +{ + QList tabIdsToSave; + for (int i = 0; i < mFileList->rowCount(); i++) { + if (mFileList->item(i, 0)->checkState() == Qt::Checked) { + qDebug() << mFileList->item(i, 2)->text(); + tabIdsToSave << mFileList->item(i, 2)->text().toInt(); + } + } + return tabIdsToSave; +} + diff --git a/quitdialog.h b/quitdialog.h new file mode 100755 index 0000000..0dd4bf5 --- /dev/null +++ b/quitdialog.h @@ -0,0 +1,61 @@ +/* + * keymgmt.h + * + * Copyright 2008 gpg4usb-team + * + * 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 of the License, or + * (at your option) any later version. + * + * 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, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + */ + +#ifndef __QUITDIALOG_H__ +#define __QUITDIALOG_H__ + + +class QMainWindow; +class QWidget; +class iostream; +class QtGui; +class QString; +class QFileDialog; +class QStringList; +class QIcon; +class QAction; +class QMenu; +class QApplication; +class QProgresssBar; + +#include "keygenthread.h" +#include "context.h" +class QuitDialog : public QDialog +{ + Q_OBJECT + +public: + QuitDialog(QWidget *parent,QHash unsavedDocs, QString iconPath); + bool isDiscarded(); + QList getTabIdsToSave(); + +private slots: +// void accept(); + void myDiscard(); + +private: + QAction *closeAct; + QLabel *nameLabel; + bool discarded; + QTableWidget *mFileList; +}; + +#endif // __QUITDIALOG_H__ diff --git a/release/icons/error.png b/release/icons/error.png new file mode 100755 index 0000000..d64c533 Binary files /dev/null and b/release/icons/error.png differ diff --git a/textedit.cpp b/textedit.cpp index 84a2622..084da83 100644 --- a/textedit.cpp +++ b/textedit.cpp @@ -22,12 +22,13 @@ #include "QDebug" #include "QUrl" #include "textedit.h" - +#include "quitdialog.h" class QFileDialog; class QMessageBox; -TextEdit::TextEdit() +TextEdit::TextEdit(QString iconPath) { + mIconPath = iconPath; countPage = 0; tabWidget = new QTabWidget(this); tabWidget->setMovable(true); @@ -304,7 +305,6 @@ bool TextEdit::maybeSaveCurrentTab() { { QString filePath = page->getFilePath(); QMessageBox::StandardButton result; - result = QMessageBox::warning(this, tr("Unsaved document"), tr("The document has been modified:")+"\n"+filePath+"\n\n"+tr("Do you want to save your changes?"), QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel); @@ -324,8 +324,18 @@ bool TextEdit::maybeSaveCurrentTab() { return true; } +bool TextEdit::saveTab(int i) { + EditorPage *page = qobject_cast (tabWidget->widget(i)); + + QString filePath = page->getFilePath(); + if (filePath == "") { + return saveAs(); + } else { + return saveFile(filePath); + } + - +} /** * Checks if there are unsaved documents in any tab, * which may need to be saved. Call this function before @@ -361,7 +371,7 @@ bool TextEdit::maybeSaveAnyTab() /* * more than one unsaved documents */ - } else if(unsavedDocs.size() > 0) { + } else if(unsavedDocs.size() > 1) { QString docList; QHashIterator i (unsavedDocs); @@ -371,19 +381,29 @@ bool TextEdit::maybeSaveAnyTab() docList.append(i.value()).append("\n"); } - QMessageBox::StandardButton result; - result = QMessageBox::warning(this, tr("Unsaved documents"), - tr("There are unsaved changes in the following documents:\n") - + docList, - QMessageBox::Discard | QMessageBox::Cancel); + QuitDialog *dialog; + dialog=new QuitDialog(this, unsavedDocs, mIconPath); - if (result == QMessageBox::Discard) { - return true; + + int result = dialog->exec(); + // return true, if app can be closed + if (result == QDialog::Rejected){ + if (dialog->isDiscarded()){ + return true; + } else { + return false; + } } else { - return false; + QList tabIdsToSave = dialog->getTabIdsToSave(); + foreach (int tabId, tabIdsToSave){ + qDebug() << "handling for save" << tabId; + + } + + return true; } - /* + /* * no unsaved documents */ } else { @@ -414,7 +434,6 @@ void TextEdit::dragEnterEvent(QDragEnterEvent *event) { if (event->mimeData()->hasFormat("text/plain")) qDebug() << "enter textedit drag action"; - event->acceptProposedAction(); } @@ -503,6 +522,7 @@ void TextEdit::showModified() { } void TextEdit::switchTabUp() { + qDebug() << "hallo"; if (tabWidget->count() > 1) { if (tabWidget->count() == tabWidget->currentIndex()+1){ @@ -516,6 +536,7 @@ void TextEdit::switchTabUp() { } void TextEdit::switchTabDown() { + qDebug() << "hallo"; if (tabWidget->count() > 1) { if (tabWidget->currentIndex()==1) { @@ -526,5 +547,31 @@ void TextEdit::switchTabDown() { tabWidget->setCurrentIndex(tabWidget->currentIndex()-1); } } + this->removeTab(0);; +} + +int TextEdit::getUnsavedDocumentsNumber() { + int number=0; + for(int i=0; i < tabWidget->count(); i++) { + EditorPage *ep = qobject_cast (tabWidget->widget(i)); + if(ep->getTextPage()->document()->isModified()) { + number++; + } + } + return number; } +QHash TextEdit::unsavedDocuments() { + QHash unsavedDocs; // this list could be used to implement gedit like "unsaved changed"-dialog + + for(int i=0; i < tabWidget->count(); i++) { + EditorPage *ep = qobject_cast (tabWidget->widget(i)); + if(ep->getTextPage()->document()->isModified()) { + QString docname = tabWidget->tabText(i); + // remove * before name of modified doc + docname.remove(0,2); + unsavedDocs.insert(i, docname); + } + } + return unsavedDocs; +} diff --git a/textedit.h b/textedit.h index 39ed9e9..3a66175 100644 --- a/textedit.h +++ b/textedit.h @@ -40,11 +40,13 @@ class TextEdit : public QWidget { Q_OBJECT public: - TextEdit(); + TextEdit(QString iconPath); void loadFile(const QString &fileName); bool maybeSaveAnyTab(); bool maybeSaveCurrentTab(); QPlainTextEdit* curTextPage(); + int getUnsavedDocumentsNumber(); + QHash unsavedDocuments(); public slots: void quote(); @@ -60,11 +62,13 @@ public slots: private: QString strippedName(const QString &fullFileName); - int countPage; - QTabWidget *tabWidget; bool maybeSaveFile(); EditorPage *curPage(); void setCursorPosition(); + QString mIconPath; + int countPage; + QTabWidget *tabWidget; + bool saveTab(int i); private slots: void removeTab(int index); -- cgit v1.2.3