aboutsummaryrefslogtreecommitdiffstats
path: root/src/ui/widgets/TextEdit.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/ui/widgets/TextEdit.cpp')
-rw-r--r--src/ui/widgets/TextEdit.cpp168
1 files changed, 95 insertions, 73 deletions
diff --git a/src/ui/widgets/TextEdit.cpp b/src/ui/widgets/TextEdit.cpp
index 49049baa..643747ed 100644
--- a/src/ui/widgets/TextEdit.cpp
+++ b/src/ui/widgets/TextEdit.cpp
@@ -24,7 +24,7 @@
#include "ui/widgets/TextEdit.h"
-TextEdit::TextEdit() {
+TextEdit::TextEdit(QWidget *parent) : QWidget(parent) {
countPage = 0;
tabWidget = new QTabWidget(this);
tabWidget->setMovable(true);
@@ -66,53 +66,83 @@ void TextEdit::slotNewHelpTab(const QString &title, const QString &path) const {
void TextEdit::slotNewFileTab() const {
- auto *page = new FilePage();
- tabWidget->addTab(page, "File");
+ auto *page = new FilePage(qobject_cast<QWidget *>(parent()));
+ tabWidget->addTab(page, "[File Browser]");
tabWidget->setCurrentIndex(tabWidget->count() - 1);
+ connect(page, SIGNAL(pathChanged(const QString &)), this, SLOT(slotFilePagePathChanged(const QString &)));
}
+void TextEdit::slotOpenFile(QString &path) {
+
+ QFile file(path);
+
+ if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
+ auto *page = new EditorPage(path);
+
+ QTextStream in(&file);
+ QApplication::setOverrideCursor(Qt::WaitCursor);
+ page->getTextPage()->setPlainText(in.readAll());
+ page->setFilePath(path);
+ QTextDocument *document = page->getTextPage()->document();
+ document->setModified(false);
+
+ tabWidget->addTab(page, strippedName(path));
+ tabWidget->setCurrentIndex(tabWidget->count() - 1);
+ QApplication::restoreOverrideCursor();
+ page->getTextPage()->setFocus();
+ connect(page->getTextPage()->document(), SIGNAL(modificationChanged(bool)), this,
+ SLOT(slotShowModified()));
+ //enableAction(true)
+ file.close();
+ } else {
+ QMessageBox::warning(this, tr("Warning"),
+ tr("Cannot read file %1:\n%2.")
+ .arg(path).arg(file.errorString()));
+ }
+}
+
void TextEdit::slotOpen() {
QStringList fileNames = QFileDialog::getOpenFileNames(this, tr("Open file"),
QDir::currentPath());
- foreach (QString fileName, fileNames) {
- if (!fileName.isEmpty()) {
- QFile file(fileName);
-
- if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
- auto *page = new EditorPage(fileName);
-
- QTextStream in(&file);
- QApplication::setOverrideCursor(Qt::WaitCursor);
- page->getTextPage()->setPlainText(in.readAll());
- page->setFilePath(fileName);
- QTextDocument *document = page->getTextPage()->document();
- document->setModified(false);
-
- tabWidget->addTab(page, strippedName(fileName));
- tabWidget->setCurrentIndex(tabWidget->count() - 1);
- QApplication::restoreOverrideCursor();
- page->getTextPage()->setFocus();
- connect(page->getTextPage()->document(), SIGNAL(modificationChanged(bool)), this,
- SLOT(slotShowModified()));
- //enableAction(true)
- file.close();
- } else {
- QMessageBox::warning(this, tr("Application"),
- tr("Cannot read file %1:\n%2.")
- .arg(fileName)
- .arg(file.errorString()));
- }
+ for (const auto &fileName : fileNames) {
+ if (!fileName.isEmpty()) {
+ QFile file(fileName);
+
+ if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
+ auto *page = new EditorPage(fileName);
+
+ QTextStream in(&file);
+ QApplication::setOverrideCursor(Qt::WaitCursor);
+ page->getTextPage()->setPlainText(in.readAll());
+ page->setFilePath(fileName);
+ QTextDocument *document = page->getTextPage()->document();
+ document->setModified(false);
+
+ tabWidget->addTab(page, strippedName(fileName));
+ tabWidget->setCurrentIndex(tabWidget->count() - 1);
+ QApplication::restoreOverrideCursor();
+ page->getTextPage()->setFocus();
+ connect(page->getTextPage()->document(), SIGNAL(modificationChanged(bool)), this,
+ SLOT(slotShowModified()));
+ //enableAction(true)
+ file.close();
+ } else {
+ QMessageBox::warning(this, tr("Warning"),
+ tr("Cannot read file %1:\n%2.")
+ .arg(fileName)
+ .arg(file.errorString()));
}
}
+ }
}
void TextEdit::slotSave() {
- if (tabWidget->count() == 0 || slotCurPage() == 0) {
+ if (tabWidget->count() == 0 || slotCurPageTextEdit() == 0) {
return;
}
- QString fileName = slotCurPage()->getFilePath();
+ QString fileName = slotCurPageTextEdit()->getFilePath();
if (fileName.isEmpty()) {
//QString docname = tabWidget->tabText(tabWidget->currentIndex());
@@ -131,7 +161,7 @@ bool TextEdit::saveFile(const QString &fileName) {
QFile file(fileName);
if (file.open(QIODevice::WriteOnly | QIODevice::Text)) {
- EditorPage *page = slotCurPage();
+ EditorPage *page = slotCurPageTextEdit();
QTextStream outputStream(&file);
QApplication::setOverrideCursor(Qt::WaitCursor);
@@ -158,11 +188,11 @@ bool TextEdit::saveFile(const QString &fileName) {
bool TextEdit::slotSaveAs() {
- if (tabWidget->count() == 0 || slotCurPage() == 0) {
+ if (tabWidget->count() == 0 || slotCurPageTextEdit() == 0) {
return true;
}
- EditorPage *page = slotCurPage();
+ EditorPage *page = slotCurPageTextEdit();
QString path;
if (page->getFilePath() != "") {
path = page->getFilePath();
@@ -170,7 +200,7 @@ bool TextEdit::slotSaveAs() {
path = tabWidget->tabText(tabWidget->currentIndex()).remove(0, 2);
}
- QString fileName = QFileDialog::getSaveFileName(this, tr("Save file "),
+ QString fileName = QFileDialog::getSaveFileName(this, tr("Save file"),
path);
return saveFile(fileName);
}
@@ -178,7 +208,7 @@ bool TextEdit::slotSaveAs() {
void TextEdit::slotCloseTab() {
removeTab(tabWidget->currentIndex());
if (tabWidget->count() != 0) {
- slotCurPage()->getTextPage()->setFocus();
+ slotCurPageTextEdit()->getTextPage()->setFocus();
}
}
@@ -218,7 +248,7 @@ void TextEdit::removeTab(int index) {
*/
bool TextEdit::maybeSaveCurrentTab(bool askToSave) {
- EditorPage *page = slotCurPage();
+ EditorPage *page = slotCurPageTextEdit();
// if this page is no textedit, there should be nothing to save
if (page == nullptr) {
return true;
@@ -299,7 +329,8 @@ bool TextEdit::maybeSaveAnyTab() {
bool allsaved = true;
QList<int> tabIdsToSave = dialog->getTabIdsToSave();
- foreach (int tabId, tabIdsToSave) {
+ foreach(int
+ tabId, tabIdsToSave) {
tabWidget->setCurrentIndex(tabId);
if (!maybeSaveCurrentTab(false)) {
allsaved = false;
@@ -326,10 +357,10 @@ QTextEdit *TextEdit::curTextPage() const {
}
}
-QTextBrowser *TextEdit::curHelpPage() const {
- auto *curHelpPage = qobject_cast<HelpPage *>(tabWidget->currentWidget());
- if (curHelpPage != nullptr) {
- return curHelpPage->getBrowser();
+FilePage * TextEdit::curFilePage() const {
+ auto *curFilePage = qobject_cast<FilePage *>(tabWidget->currentWidget());
+ if (curFilePage != nullptr) {
+ return curFilePage;
} else {
return nullptr;
}
@@ -339,11 +370,16 @@ int TextEdit::tabCount() const {
return tabWidget->count();
}
-EditorPage *TextEdit::slotCurPage() const {
+EditorPage *TextEdit::slotCurPageTextEdit() const {
auto *curPage = qobject_cast<EditorPage *>(tabWidget->currentWidget());
return curPage;
}
+FilePage *TextEdit::slotCurPageFileTreeView() const {
+ auto *curPage = qobject_cast<FilePage *>(tabWidget->currentWidget());
+ return curPage;
+}
+
void TextEdit::slotQuote() const {
if (tabWidget->count() == 0 || curTextPage() == nullptr) {
return;
@@ -386,7 +422,7 @@ void TextEdit::loadFile(const QString &fileName) {
QApplication::setOverrideCursor(Qt::WaitCursor);
curTextPage()->setPlainText(in.readAll());
QApplication::restoreOverrideCursor();
- slotCurPage()->setFilePath(fileName);
+ slotCurPageTextEdit()->setFilePath(fileName);
tabWidget->setTabText(tabWidget->currentIndex(), strippedName(fileName));
file.close();
// statusBar()->showMessage(tr("File loaded"), 2000);
@@ -403,9 +439,7 @@ void TextEdit::slotPrint() {
#ifndef QT_NO_PRINTER
QTextDocument *document;
- if (curTextPage() == nullptr) {
- document = curHelpPage()->document();
- } else {
+ if (curTextPage() != nullptr) {
document = curTextPage()->document();
}
QPrinter printer;
@@ -479,8 +513,6 @@ void TextEdit::slotCopy() const {
if (curTextPage() != nullptr) {
curTextPage()->copy();
- } else {
- curHelpPage()->copy();
}
@@ -517,8 +549,6 @@ void TextEdit::slotZoomIn() const {
if (curTextPage() != nullptr) {
curTextPage()->zoomIn();
- } else {
- curHelpPage()->zoomIn();
}
}
@@ -530,8 +560,6 @@ void TextEdit::slotZoomOut() const {
if (curTextPage() != nullptr) {
curTextPage()->zoomOut();
- } else {
- curHelpPage()->zoomOut();
}
}
@@ -539,26 +567,20 @@ void TextEdit::slotSelectAll() const {
if (tabWidget->count() == 0 || curTextPage() == nullptr) {
return;
}
-
curTextPage()->selectAll();
}
-/*void TextEdit::dragEnterEvent(QDragEnterEvent *event)
-{
- if (event->mimeData()->hasFormat("text/plain"))
- qDebug() << "enter textedit drag action";
- event->acceptProposedAction();
-}
-
-void TextEdit::dropEvent(QDropEvent* event)
-{
- curTextPage()->setPlainText(event->mimeData()->text());
-
- foreach (QUrl tmp, event->mimeData()->urls())
- {
- qDebug() << tmp;
+void TextEdit::slotFilePagePathChanged(const QString &path) {
+ int index = tabWidget->currentIndex();
+ QString mPath;
+ QFileInfo fileInfo(path);
+ QString tPath = fileInfo.path();
+ if (path.size() > 18) {
+ mPath = tPath.mid(tPath.size() - 18, 18).prepend("...");
+ } else {
+ mPath = tPath;
}
-
- //event->acceptProposedAction();
+ mPath.prepend("[File Browser] ");
+ mPath.append("/");
+ tabWidget->setTabText(index, mPath);
}
-*/