diff options
author | saturneric <[email protected]> | 2024-11-25 21:46:04 +0000 |
---|---|---|
committer | saturneric <[email protected]> | 2024-11-25 21:46:04 +0000 |
commit | 21bbd04e05790de5a5feed41ea4d6dbd4a06ad24 (patch) | |
tree | 1ad5d897f31cfcf7bafbb418ca7f36b483ef7bd3 | |
parent | fix: correct spelling mistake (diff) | |
download | GpgFrontend-21bbd04e05790de5a5feed41ea4d6dbd4a06ad24.tar.gz GpgFrontend-21bbd04e05790de5a5feed41ea4d6dbd4a06ad24.zip |
feat: support drop down and open directory
-rw-r--r-- | src/ui/widgets/FileTreeView.h | 6 | ||||
-rw-r--r-- | src/ui/widgets/TextEdit.cpp | 29 | ||||
-rw-r--r-- | src/ui/widgets/TextEdit.h | 2 | ||||
-rw-r--r-- | src/ui/widgets/TextEditTabWidget.cpp | 75 | ||||
-rw-r--r-- | src/ui/widgets/TextEditTabWidget.h | 13 |
5 files changed, 80 insertions, 45 deletions
diff --git a/src/ui/widgets/FileTreeView.h b/src/ui/widgets/FileTreeView.h index 41ef014d..d99ff605 100644 --- a/src/ui/widgets/FileTreeView.h +++ b/src/ui/widgets/FileTreeView.h @@ -33,6 +33,12 @@ namespace GpgFrontend::UI { class FileTreeView : public QTreeView { Q_OBJECT public: + /** + * @brief Construct a new File Tree View object + * + * @param parent + * @param target_path + */ explicit FileTreeView(QWidget* parent, const QString& target_path); /** diff --git a/src/ui/widgets/TextEdit.cpp b/src/ui/widgets/TextEdit.cpp index a742d49d..d0cfe58e 100644 --- a/src/ui/widgets/TextEdit.cpp +++ b/src/ui/widgets/TextEdit.cpp @@ -72,18 +72,11 @@ void TextEdit::SlotNewHelpTab(const QString& title, const QString& path) const { } void TextEdit::SlotNewFileTab() { - auto const target_dir = QFileDialog::getExistingDirectory( + auto const target_directory = QFileDialog::getExistingDirectory( this, tr("Open Directory"), QDir::home().path(), QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks); - if (target_dir.isEmpty()) return; - - auto* page = new FilePage(qobject_cast<QWidget*>(parent()), target_dir); - auto index = tab_widget_->addTab(page, QString()); - tab_widget_->setTabIcon(index, QIcon(":/icons/file-browser.png")); - tab_widget_->setCurrentIndex(tab_widget_->count() - 1); - connect(page, &FilePage::SignalPathChanged, this, - &TextEdit::slot_file_page_path_changed); - page->SlotGoPath(); + if (target_directory.isEmpty()) return; + tab_widget_->SlotOpenDirectory(target_directory); } void TextEdit::SlotOpenFile(const QString& path) { @@ -556,20 +549,4 @@ void TextEdit::SlotSelectAll() const { } CurTextPage()->GetTextPage()->selectAll(); } - -void TextEdit::slot_file_page_path_changed(const QString& path) const { - int index = tab_widget_->currentIndex(); - QString m_path; - QFileInfo file_info(path); - QString t_path = file_info.absoluteFilePath(); - if (path.size() > 18) { - m_path = t_path.mid(t_path.size() - 18, 18).prepend("..."); - } else { - m_path = t_path; - } - tab_widget_->setTabText(index, m_path); - - emit UISignalStation::GetInstance() - -> SignalMainWindowlUpdateBasicalOperaMenu(0); -} } // namespace GpgFrontend::UI diff --git a/src/ui/widgets/TextEdit.h b/src/ui/widgets/TextEdit.h index 4b893a3a..5219196f 100644 --- a/src/ui/widgets/TextEdit.h +++ b/src/ui/widgets/TextEdit.h @@ -275,8 +275,6 @@ class TextEdit : public QWidget { private slots: - void slot_file_page_path_changed(const QString& path) const; - /** * @details Remove the tab with given index * diff --git a/src/ui/widgets/TextEditTabWidget.cpp b/src/ui/widgets/TextEditTabWidget.cpp index c782e868..110302be 100644 --- a/src/ui/widgets/TextEditTabWidget.cpp +++ b/src/ui/widgets/TextEditTabWidget.cpp @@ -30,6 +30,7 @@ #include "core/function/GlobalSettingStation.h" #include "core/model/CacheObject.h" +#include "ui/UISignalStation.h" #include "ui/widgets/PlainTextEditorPage.h" #include "widgets/FilePage.h" @@ -60,25 +61,38 @@ void TextEditTabWidget::dropEvent(QDropEvent* event) { continue; } - QFile file(local_file); - if (!file.open(QIODevice::ReadOnly)) { - QMessageBox::warning( - this, tr("File Open Error"), - tr("The file \"%1\" could not be opened.").arg(file_info.fileName())); - continue; - } - QByteArray file_data = file.read(1024); - file.close(); - - if (file_data.contains('\0')) { - QMessageBox::warning(this, tr("Binary File Detected"), - tr("The file \"%1\" appears to be a binary file " - "and will not be opened.") - .arg(file_info.fileName())); - continue; + if (file_info.isFile()) { + QFile file(local_file); + if (!file.open(QIODevice::ReadOnly)) { + QMessageBox::warning(this, tr("File Open Error"), + tr("The file \"%1\" could not be opened.") + .arg(file_info.fileName())); + continue; + } + QByteArray file_data = file.read(1024); + file.close(); + + if (file_data.contains('\0')) { + QMessageBox::warning(this, tr("Binary File Detected"), + tr("The file \"%1\" appears to be a binary file " + "and will not be opened.") + .arg(file_info.fileName())); + continue; + } + + SlotOpenFile(local_file); } - SlotOpenFile(local_file); + if (file_info.isDir()) { + if (!file_info.isReadable()) { + QMessageBox::warning( + this, tr("Directory Permission Denied"), + tr("You do not have permission to access the directory \"%1\".") + .arg(file_info.fileName())); + continue; + } + SlotOpenDirectory(file_info.absoluteFilePath()); + } } event->acceptProposedAction(); @@ -239,4 +253,31 @@ void TextEditTabWidget::SlotNewTabWithContent(QString title, // set content with modified status page->GetTextPage()->document()->setPlainText(content); } + +void TextEditTabWidget::SlotOpenDirectory(const QString& target_directory) { + auto* page = new FilePage(qobject_cast<QWidget*>(parent()), target_directory); + auto index = this->addTab(page, QString()); + this->setTabIcon(index, QIcon(":/icons/file-browser.png")); + this->setCurrentIndex(this->count() - 1); + connect(page, &FilePage::SignalPathChanged, this, + &TextEditTabWidget::slot_file_page_path_changed); + page->SlotGoPath(); +} + +void TextEditTabWidget::slot_file_page_path_changed(const QString& path) { + int index = this->currentIndex(); + QString m_path; + QFileInfo file_info(path); + QString t_path = file_info.absoluteFilePath(); + if (path.size() > 18) { + m_path = t_path.mid(t_path.size() - 18, 18).prepend("..."); + } else { + m_path = t_path; + } + this->setTabText(index, m_path); + + emit UISignalStation::GetInstance() + -> SignalMainWindowlUpdateBasicalOperaMenu(0); +} + } // namespace GpgFrontend::UI diff --git a/src/ui/widgets/TextEditTabWidget.h b/src/ui/widgets/TextEditTabWidget.h index 7c83a721..b0cafae2 100644 --- a/src/ui/widgets/TextEditTabWidget.h +++ b/src/ui/widgets/TextEditTabWidget.h @@ -60,6 +60,12 @@ class TextEditTabWidget : public QTabWidget { void SlotOpenFile(const QString& path); /** + * @brief + * + */ + void SlotOpenDirectory(const QString& target_directory); + + /** * @details put a * in front of current tabs title, if current textedit is * modified */ @@ -108,6 +114,13 @@ class TextEditTabWidget : public QTabWidget { */ void slot_save_status_to_cache_for_recovery(); + /** + * @brief + * + * @param path + */ + void slot_file_page_path_changed(const QString& path); + private: int count_page_ = 0; int text_page_data_modified_count_ = 0; |