aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--gpg4usb.pro6
-rw-r--r--gpgwin.cpp19
-rw-r--r--textedit.cpp383
-rw-r--r--textedit.h28
4 files changed, 355 insertions, 81 deletions
diff --git a/gpg4usb.pro b/gpg4usb.pro
index 0971ea6..25265f1 100644
--- a/gpg4usb.pro
+++ b/gpg4usb.pro
@@ -26,7 +26,8 @@ HEADERS += attachments.h \
keydetailsdialog.h \
settingsdialog.h \
attachmenttablemodel.h \
- textedit.h
+ textedit.h \
+ editorpage.h
SOURCES += attachments.cpp \
context.cpp \
gpgwin.cpp \
@@ -39,7 +40,8 @@ SOURCES += attachments.cpp \
keydetailsdialog.cpp \
settingsdialog.cpp \
attachmenttablemodel.cpp \
- textedit.cpp
+ textedit.cpp \
+ editorpage.cpp
RC_FILE = gpg4usb.rc
# comment out line below for static building
diff --git a/gpgwin.cpp b/gpgwin.cpp
index b76b72f..d33b54c 100644
--- a/gpgwin.cpp
+++ b/gpgwin.cpp
@@ -437,16 +437,17 @@ void GpgWin::encrypt()
QStringList *uidList = mKeyList->getChecked();
QByteArray *tmp = new QByteArray();
- if (mCtx->encrypt(uidList, edit->toPlainText().toUtf8(), tmp)) {
+// if (mCtx->encrypt(uidList, edit.curTextPage.toPlainText().toUtf8(), tmp)) {
+ if (mCtx->encrypt(uidList, edit->curTextPage()->toPlainText().toUtf8(), tmp)) {
QString *tmp2 = new QString(*tmp);
- edit->setPlainText(*tmp2);
+ edit->curTextPage()->setPlainText(*tmp2);
}
}
void GpgWin::decrypt()
{
QByteArray *decrypted = new QByteArray();
- QByteArray text = edit->toPlainText().toAscii(); // TODO: toUtf8() here?
+ QByteArray text = edit->curTextPage()->toPlainText().toAscii(); // TODO: toUtf8() here?
preventNoDataErr(&text);
mCtx->decrypt(text, decrypted);
if (!decrypted->isEmpty()) {
@@ -479,7 +480,7 @@ void GpgWin::decrypt()
}
}
- edit->setPlainText(QString::fromUtf8(*decrypted));
+ edit->curTextPage()->setPlainText(QString::fromUtf8(*decrypted));
//edit->setPlainText(*decrypted);
}
}
@@ -566,7 +567,7 @@ void GpgWin::preventNoDataErr(QByteArray *in)
void GpgWin::importKeyFromEdit()
{
- mCtx->importKey(edit->toPlainText().toAscii());
+ mCtx->importKey(edit->curTextPage()->toPlainText().toAscii());
}
void GpgWin::importKeyFromClipboard()
@@ -613,15 +614,15 @@ void GpgWin::sign() {
QStringList *uidList = mKeyList->getChecked();
QByteArray *tmp = new QByteArray();
- if (mCtx->sign(uidList, edit->toPlainText().toUtf8(), tmp)) {
+ if (mCtx->sign(uidList, edit->curTextPage()->toPlainText().toUtf8(), tmp)) {
QString *tmp2 = new QString(*tmp);
- edit->setPlainText(*tmp2);
+ edit->curTextPage()->setPlainText(*tmp2);
}
}
void GpgWin::verify() {
- mCtx->verify(edit->toPlainText().toUtf8());
+ mCtx->verify(edit->curTextPage()->toPlainText().toUtf8());
}
@@ -673,7 +674,7 @@ void GpgWin::appendSelectedKeys()
QByteArray *keyArray = new QByteArray();
mCtx->exportKeys(mKeyList->getSelected(), keyArray);
- edit->appendPlainText(*keyArray);
+ edit->curTextPage()->appendPlainText(*keyArray);
}
void GpgWin::fileEncryption()
diff --git a/textedit.cpp b/textedit.cpp
index 07d3fbe..dc183dc 100644
--- a/textedit.cpp
+++ b/textedit.cpp
@@ -21,15 +21,325 @@
#include "QDebug"
#include "QUrl"
+#include "textedit.h"
+
class QFileDialog;
class QMessageBox;
-#include "textedit.h"
-TextEdit::TextEdit(QWidget *parent)
+TextEdit::TextEdit()
{
+ countPage = 0;
+ tabWidget = new QTabWidget(this);
+ tabWidget->setMovable(true);
+ tabWidget->setTabsClosable(true);
+ tabWidget->setDocumentMode(true);
+
+ QVBoxLayout *layout = new QVBoxLayout;
+ layout->addWidget(tabWidget);
+ layout->setContentsMargins(0, 0, 0, 0);
+ layout->setSpacing(0);
+ setLayout(layout);
+
+ connect(tabWidget, SIGNAL(tabCloseRequested(int)), this, SLOT(removeTab(int)));
+// connect(tabWidget, SIGNAL(currentChanged(int)), this, SLOT(curSyntaxHiglight()));
+ newFile();
setAcceptDrops(true);
}
+
+/*void TextEditor::closeEvent(QCloseEvent *event)
+{
+ int curIndex = tabWidget->count();
+ bool answ = true;
+
+
+ while (curIndex >= 1 && answ == true)
+ {
+ answ = closeFile();
+
+ curIndex--;
+ }
+
+
+ if (answ == true)
+ {
+ writeSettings();
+ event->accept();
+ }
+ else
+ {
+ event->ignore();
+ }
+}
+*/
+void TextEdit::newFile()
+{
+ QString header = "new " +
+ QString::number(++countPage);
+
+ tabWidget->addTab(new EditorPage(), header);
+ tabWidget->setCurrentIndex(tabWidget->count() - 1);
+
+// setCursorPosition();
+ }
+
+
+void TextEdit::open()
+{
+ QString fileName = QFileDialog::getOpenFileName(this, tr("Open file"),
+ QDir::currentPath());
+ setCurrentFile(fileName);
+
+ if (!fileName.isEmpty())
+ {
+ EditorPage *page = new EditorPage(fileName);
+ QFile file(fileName);
+
+ if (file.open(QIODevice::ReadOnly | QIODevice::Text))
+ {
+ QTextStream in(&file);
+ QApplication::setOverrideCursor(Qt::WaitCursor);
+ page->getTextPage()->setPlainText(in.readAll());
+
+ QTextDocument *document = page->getTextPage()->document();
+ document->setModified(false);
+
+ tabWidget->addTab(page, strippedName(fileName));
+ tabWidget->setCurrentIndex(tabWidget->count() - 1);
+ QApplication::restoreOverrideCursor();
+ // setCursorPosition();
+ //enableAction(true);
+ }
+ else
+ {
+ QMessageBox::warning(this, tr("Application"),
+ tr("Cannot read file %1:\n%2.")
+ .arg(fileName)
+ .arg(file.errorString()));
+ }
+ }
+}
+
+
+void TextEdit::save()
+{
+ QString fileName = curPage()->getFilePath();
+
+ // åñëè òåêóùàÿ ñòðàíèöà íå ñîäåðæèò
+ // ïóòè ê ôàéëó òî
+ if (fileName.isEmpty())
+ {
+ // ñîõðàíÿåì åå ïîä íîâûì èìåíåì
+ saveAs();
+ }
+ else
+ {
+ // èíà÷å ñîõðàíÿåì ôàéë
+ // ïîä òåêóùåì èìåíåì
+ saveFile(fileName);
+ }
+}
+
+
+bool TextEdit::saveFile(const QString &fileName)
+{
+ if (fileName.isEmpty())
+ {
+ return false;
+ }
+
+
+ QFile textFile(fileName);
+
+ // åñëè óäàëîñü îòêðûòü ôàéë òî
+ if (textFile.open(QIODevice::WriteOnly | QIODevice::Text))
+ {
+ QTextStream outputStream(&textFile);
+ EditorPage *page = curPage();
+
+ // çàïèñûâàåì â íåãî âåñü òåêñò,
+ // êîòîðûé áûë íà ñòðàíèöå
+ outputStream << page->getTextPage()->toPlainText();
+
+ // ïîìå÷àåì äîêóìåíò ÷òî îí íå èçìåíÿëñÿ
+ QTextDocument *document = page->getTextPage()->document();
+ document->setModified(false);
+
+ int curIndex = tabWidget->currentIndex();
+ tabWidget->setTabText(curIndex, strippedName(fileName));
+
+ // statusBar()->showMessage(tr("File saved"), 2000);
+
+ return true;
+ }
+ else
+ {
+ // statusBar()->showMessage(tr("Error save file"), 2000);
+
+ return false;
+ }
+}
+
+
+bool TextEdit::saveAs()
+{
+ QString fileName = QFileDialog::getSaveFileName(this, tr("Save file"),
+ QDir::currentPath());
+
+ return saveFile(fileName);
+}
+
+
+bool TextEdit::closeFile()
+{
+ if (tabWidget->count() != 0)
+ {
+
+ if (maybeSave())
+ {
+ int tabIndex = tabWidget->currentIndex();
+ tabWidget->setCurrentIndex(tabIndex);
+
+ curPage()->close();
+
+ tabWidget->removeTab(tabIndex);
+
+
+ if (tabWidget->count() == 0)
+ {
+ // enableAction(false);
+ }
+
+ return true;
+ }
+
+ return false;
+ }
+ return false;
+}
+
+
+void TextEdit::removeTab(int index)
+{
+ if (tabWidget->count() != 0)
+ {
+ if (maybeSave())
+ {
+ tabWidget->setCurrentIndex(index);
+
+ curPage()->close();
+
+ tabWidget->removeTab(index);
+ }
+ }
+
+
+ if (tabWidget->count() == 0)
+ {
+ // enableAction(false);
+ }
+}
+
+void TextEdit::cut()
+{
+ curTextPage()->cut();
+}
+
+
+void TextEdit::copy()
+{
+ curTextPage()->copy();
+}
+
+
+void TextEdit::paste()
+{
+ curTextPage()->paste();
+}
+
+
+void TextEdit::undo()
+{
+ curTextPage()->undo();
+}
+
+
+void TextEdit::redo()
+{
+ curTextPage()->redo();
+}
+
+void TextEdit::selectAll()
+{
+ curTextPage()->selectAll();
+}
+
+bool TextEdit::maybeSave()
+{
+ EditorPage *page = curPage();
+
+ // åñëè íå îñòàëîñü íè îäíîé çàêëàäêè òî
+ if (page == 0)
+ {
+ return false;
+ }
+
+ QTextDocument *document = page->getTextPage()->document();
+
+ // åñëè áûëè èçìåíåíèÿ â òåêñòå òî
+ if (document->isModified())
+ {
+ int result = QMessageBox::information(this, tr("Save"),tr("Save file ?"),
+ QMessageBox::Yes, QMessageBox::No,
+ QMessageBox::Cancel);
+
+
+ if (result == QMessageBox::Yes)
+ {
+ // ïîëó÷àåì ïóòü äî ôàéëà êîòîðûé õðàíèò êàæäàÿ ñòðàíèöà
+ QString filePath = page->getFilePath();
+
+ // åñëè ýòî íîâûé ôàéë è ïóòè ó íåãî íåò, òî
+ if (filePath == "")
+ {
+ // äàåì ïîëüçîâàòåëþ ñîõðàíèòü åãî
+ // ïîä íîâûì èìåíåì
+ return saveAs();
+ }
+ else
+ {
+ // èíà÷å ñîõðàíÿåì ñ òåêóùèì èìåíåì
+ return saveFile(filePath);
+ }
+ }
+ else if (result == QMessageBox::No)
+ {
+ return true;
+ }
+ else
+ {
+ return false;
+ }
+ }
+ return true;
+}
+
+
+QPlainTextEdit* TextEdit::curTextPage()
+{
+ EditorPage *curTextPage = qobject_cast<EditorPage *>(tabWidget->currentWidget());
+
+ return curTextPage->getTextPage();
+}
+
+
+EditorPage* TextEdit::curPage()
+{
+ EditorPage *curPage = qobject_cast<EditorPage *>(tabWidget->currentWidget());
+
+ return curPage;
+}
+
void TextEdit::dragEnterEvent(QDragEnterEvent *event)
{
if (event->mimeData()->hasFormat("text/plain"))
@@ -40,7 +350,7 @@ void TextEdit::dragEnterEvent(QDragEnterEvent *event)
void TextEdit::dropEvent(QDropEvent* event)
{
- this->setPlainText(event->mimeData()->text());
+ curTextPage()->setPlainText(event->mimeData()->text());
qDebug() << "enter textedit drop action";
qDebug() << event->mimeData()->text();
@@ -54,8 +364,7 @@ void TextEdit::dropEvent(QDropEvent* event)
void TextEdit::quote()
{
-
- QTextCursor cursor(this->document());
+ QTextCursor cursor(curTextPage()->document());
// beginEditBlock and endEditBlock() let operation look like single undo/redo operation
cursor.beginEditBlock();
@@ -77,32 +386,6 @@ bool TextEdit::isKey(QString key)
return true;
}
-void TextEdit::open()
-{
- if (maybeSave()) {
- QString fileName = QFileDialog::getOpenFileName(this);
- if (!fileName.isEmpty())
- loadFile(fileName);
- }
-}
-
-bool TextEdit::save()
-{
- if (curFile.isEmpty()) {
- return saveAs();
- } else {
- return saveFile(curFile);
- }
-}
-
-bool TextEdit::saveAs()
-{
- QString fileName = QFileDialog::getSaveFileName(this);
- if (fileName.isEmpty())
- return false;
-
- return saveFile(fileName);
-}
void TextEdit::loadFile(const QString &fileName)
{
@@ -116,7 +399,7 @@ void TextEdit::loadFile(const QString &fileName)
}
QTextStream in(&file);
QApplication::setOverrideCursor(Qt::WaitCursor);
- this->setPlainText(in.readAll());
+ curTextPage()->setPlainText(in.readAll());
QApplication::restoreOverrideCursor();
setCurrentFile(fileName);
@@ -126,7 +409,7 @@ void TextEdit::loadFile(const QString &fileName)
void TextEdit::setCurrentFile(const QString &fileName)
{
curFile = fileName;
- this->document()->setModified(false);
+ curTextPage()->document()->setModified(false);
setWindowModified(false);
QString shownName;
@@ -143,42 +426,10 @@ QString TextEdit::strippedName(const QString &fullFileName)
return QFileInfo(fullFileName).fileName();
}
-bool TextEdit::maybeSave()
-{
- if (this->document()->isModified()) {
- QMessageBox::StandardButton ret;
- ret = QMessageBox::warning(this, tr("Application"),
- tr("The document has been modified.\nDo you want to save your changes?"),
- QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
- if (ret == QMessageBox::Save)
- return save();
- else if (ret == QMessageBox::Cancel)
- return false;
- }
- return true;
-}
-
-bool TextEdit::saveFile(const QString &fileName)
-{
- QFile file(fileName);
- if (!file.open(QFile::WriteOnly | QFile::Text)) {
- QMessageBox::warning(this, tr("File"),
- tr("Cannot write file %1:\n%2.")
- .arg(fileName)
- .arg(file.errorString()));
- return false;
- }
- QTextStream out(&file);
- QApplication::setOverrideCursor(Qt::WaitCursor);
- out << this->toPlainText();
- QApplication::restoreOverrideCursor();
- //statusBar()->showMessage(tr("Saved '%1'").arg(fileName), 2000);
- return true;
-}
void TextEdit::print()
{
#ifndef QT_NO_PRINTER
- QTextDocument *document = this->document();
+ QTextDocument *document = curTextPage()->document();
QPrinter printer;
QPrintDialog *dlg = new QPrintDialog(&printer, this);
diff --git a/textedit.h b/textedit.h
index d2c6197..58bcaab 100644
--- a/textedit.h
+++ b/textedit.h
@@ -30,35 +30,55 @@
#include <QFileInfo>
#include <QApplication>
#include <QFile>
+#include "editorpage.h"
class QWidget;
class QString;
+class QTabWidget;
-class TextEdit : public QPlainTextEdit
+class TextEdit : public QWidget
{
Q_OBJECT
public:
- TextEdit(QWidget *parent=0);
+ TextEdit();
void setCurrentFile(const QString &fileName);
void loadFile(const QString &fileName);
bool maybeSave();
+ QPlainTextEdit* curTextPage();
public slots:
void quote();
- bool save();
+ void save();
bool saveAs();
void open();
void print();
private:
bool isKey(QString key);
- bool saveFile(const QString &fileName);
QString strippedName(const QString &fullFileName);
QString curFile;
+ int countPage;
+ QTabWidget *tabWidget;
+ bool maybeSaveFile();
+ EditorPage *curPage();
+ void setCursorPosition();
+
+private slots:
+ void removeTab(int index);
+ void cut();
+ void copy();
+ void paste();
+ void undo();
+ void redo();
+ void selectAll();
protected:
void dragEnterEvent(QDragEnterEvent *event);
void dropEvent(QDropEvent* event);
+ void newFile();
+ bool saveFile(const QString &fileName);
+ bool closeFile();
+
};
#endif // TEXTEDIT