/* * textedit.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 "QDebug" #include "QUrl" class QFileDialog; class QMessageBox; #include "textedit.h" TextEdit::TextEdit(QWidget *parent) { setAcceptDrops(true); } void TextEdit::dragEnterEvent(QDragEnterEvent *event) { if (event->mimeData()->hasFormat("text/plain")) qDebug() << "enter textedit drag action"; event->acceptProposedAction(); } void TextEdit::dropEvent(QDropEvent* event) { this->setPlainText(event->mimeData()->text()); qDebug() << "enter textedit drop action"; qDebug() << event->mimeData()->text(); foreach (QUrl tmp, event->mimeData()->urls()) { qDebug() << "hallo" << tmp; } //event->acceptProposedAction(); } void TextEdit::quote() { QTextCursor cursor(this->document()); // beginEditBlock and endEditBlock() let operation look like single undo/redo operation cursor.beginEditBlock(); cursor.setPosition(0); cursor.insertText("> "); while (!cursor.isNull() && !cursor.atEnd()) { cursor.movePosition(QTextCursor::EndOfLine); cursor.movePosition(QTextCursor::NextCharacter); if(!cursor.atEnd()) cursor.insertText("> "); } cursor.endEditBlock(); } bool TextEdit::isKey(QString key) { qDebug() << key.contains("-----BEGIN PGP PUBLIC KEY BLOCK-----", Qt::CaseSensitive); 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) { QFile file(fileName); if (!file.open(QFile::ReadOnly | QFile::Text)) { QMessageBox::warning(this, tr("Application"), tr("Cannot read file %1:\n%2.") .arg(fileName) .arg(file.errorString())); return; } QTextStream in(&file); QApplication::setOverrideCursor(Qt::WaitCursor); this->setPlainText(in.readAll()); QApplication::restoreOverrideCursor(); setCurrentFile(fileName); // statusBar()->showMessage(tr("File loaded"), 2000); } void TextEdit::setCurrentFile(const QString &fileName) { curFile = fileName; this->document()->setModified(false); setWindowModified(false); QString shownName; if (curFile.isEmpty()) shownName = "untitled.txt"; else shownName = strippedName(curFile); //setWindowTitle(tr("%1[*] - %2").arg(shownName).arg(qApp->applicationName())); } 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(); QPrinter printer; QPrintDialog *dlg = new QPrintDialog(&printer, this); if (dlg->exec() != QDialog::Accepted) return; document->print(&printer); //statusBar()->showMessage(tr("Ready"), 2000); #endif }