Skip to content

Commit

Permalink
Refactor GlobalOptionsWindow
Browse files Browse the repository at this point in the history
Signed-off-by: xiaoming <2014500726@smail.xtu.edu.cn>
  • Loading branch information
QQxiaoming committed Sep 3, 2024
1 parent 0068e4b commit 42a6bab
Show file tree
Hide file tree
Showing 9 changed files with 184 additions and 53 deletions.
32 changes: 25 additions & 7 deletions lib/qcustomfilesystemmodel/qcustomfilesystemmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ int QCustomFileSystemModel::rowCount(const QModelIndex &parent) const {
}

int QCustomFileSystemModel::columnCount(const QModelIndex &parent) const {
if(m_onlyName) return 1;
if (parent.isValid() && indexValid(parent))
return static_cast<QCustomFileSystemItem*>(parent.internalPointer())->columnCount();
else
Expand All @@ -137,6 +138,8 @@ QVariant QCustomFileSystemModel::data(const QModelIndex &index, int role) const
return QVariant();
if (role != Qt::DisplayRole && role != Qt::DecorationRole)
return QVariant();
if(m_onlyName && role == Qt::DecorationRole)
return QVariant();
QCustomFileSystemItem *item = static_cast<QCustomFileSystemItem*>(index.internalPointer());
if(item->data().toString() == "")
return QVariant();
Expand Down Expand Up @@ -259,6 +262,7 @@ QModelIndex QCustomFileSystemModel::setRootPath(const QString &path) {
QCustomFileSystemItem *rootItem = new QCustomFileSystemItem(path);
m_rootPath = path;
QStringList rootEntries = pathEntryList(m_rootPath);
QList<QCustomFileSystemItem*> allItems;
QList<QCustomFileSystemItem*> dirItems;
QList<QCustomFileSystemItem*> fileItems;
for (int i = 0; i < rootEntries.count(); ++i) {
Expand All @@ -275,20 +279,34 @@ QModelIndex QCustomFileSystemModel::setRootPath(const QString &path) {
if (isDir) {
//QStringList childEntries = pathEntryList(childPath);
//childItem->setSize(childEntries.count());
dirItems.append(childItem);
if(m_DistinguishType) {
dirItems.append(childItem);
} else {
allItems.append(childItem);
}
//add dummy item
QCustomFileSystemItem *dummyItem = new QCustomFileSystemItem("", childItem);
childItem->appendChild(dummyItem);
} else {
childItem->setSize(size);
fileItems.append(childItem);
if(m_DistinguishType) {
fileItems.append(childItem);
} else {
allItems.append(childItem);
}
}
}
foreach(QCustomFileSystemItem *item, dirItems) {
rootItem->appendChild(item);
}
foreach(QCustomFileSystemItem *item, fileItems) {
rootItem->appendChild(item);
if(m_DistinguishType) {
foreach(QCustomFileSystemItem *item, dirItems) {
rootItem->appendChild(item);
}
foreach(QCustomFileSystemItem *item, fileItems) {
rootItem->appendChild(item);
}
} else {
foreach(QCustomFileSystemItem *item, allItems) {
rootItem->appendChild(item);
}
}
QModelIndex index = createIndex(0, 0, rootItem);
m_rootItem = rootItem;
Expand Down
5 changes: 5 additions & 0 deletions lib/qcustomfilesystemmodel/qcustomfilesystemmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,18 @@ class QCustomFileSystemModel : public QAbstractItemModel

QString filePath(const QModelIndex &index);

void setOnlyName(bool onlyName) { m_onlyName = onlyName; }
void setDistinguishType(bool distinguishType) { m_DistinguishType = distinguishType; }

virtual QString separator() const = 0;
virtual QStringList pathEntryList(const QString &path) = 0;
virtual void pathInfo(QString path, bool &isDir, uint64_t &size, QDateTime &lastModified) = 0;

private:
QString m_rootPath;
QCustomFileSystemItem *m_rootItem = nullptr;
bool m_onlyName = false;
bool m_DistinguishType = true;
};

class QNativeFileSystemModel : public QCustomFileSystemModel
Expand Down
1 change: 0 additions & 1 deletion lib/qtermwidget/TerminalDisplay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ const ColorEntry base_color_table[TABLE_COLORS] =

// static
bool TerminalDisplay::_antialiasText = true;
bool TerminalDisplay::HAVE_TRANSPARENCY = true;

// we use this to force QPainter to display text in LTR mode
// more information can be found in: http://unicode.org/reports/tr9/
Expand Down
8 changes: 0 additions & 8 deletions lib/qtermwidget/TerminalDisplay.h
Original file line number Diff line number Diff line change
Expand Up @@ -416,8 +416,6 @@ class TerminalDisplay : public QWidget
/** Returns the terminal screen section which is displayed in this widget. See setScreenWindow() */
ScreenWindow* screenWindow() const;

static bool HAVE_TRANSPARENCY;

void setMotionAfterPasting(MotionAfterPasting action);
int motionAfterPasting();
void setConfirmMultilinePaste(bool confirmMultilinePaste);
Expand Down Expand Up @@ -928,12 +926,6 @@ private slots:
QWidget *messageParentWidget = nullptr;

bool _fix_quardCRT_issue33 = false;

public:
static void setTransparencyEnabled(bool enable)
{
HAVE_TRANSPARENCY = enable;
}
};

class AutoScrollHandler : public QObject
Expand Down
2 changes: 1 addition & 1 deletion src/globaloptions/globaloptionsadvancedwidget.ui
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<x>0</x>
<y>0</y>
<width>449</width>
<height>368</height>
<height>397</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
Expand Down
6 changes: 3 additions & 3 deletions src/globaloptions/globaloptionsappearancewidget.ui
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<x>0</x>
<y>0</y>
<width>550</width>
<height>501</height>
<height>528</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
Expand Down Expand Up @@ -675,14 +675,14 @@
<number>70</number>
</property>
<property name="orientation">
<enum>Qt::Horizontal</enum>
<enum>Qt::Orientation::Horizontal</enum>
</property>
</widget>
</item>
<item>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
<enum>Qt::Orientation::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
Expand Down
4 changes: 2 additions & 2 deletions src/globaloptions/globaloptionsgeneralwidget.ui
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<x>0</x>
<y>0</y>
<width>379</width>
<height>337</height>
<height>363</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout_5">
Expand Down Expand Up @@ -267,7 +267,7 @@
<item>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
<enum>Qt::Orientation::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
Expand Down
133 changes: 104 additions & 29 deletions src/globaloptions/globaloptionswindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#include <QSplitter>
#include <QTreeView>
#include <QLabel>
#include <QStringListModel>
#include <QFont>
Expand Down Expand Up @@ -46,6 +45,79 @@
#include "ui_globaloptionstransferwidget.h"
#include "ui_globaloptionsadvancedwidget.h"

GlobalOptionsModel::GlobalOptionsModel(QObject *parent)
: QCustomFileSystemModel(parent) {
}

GlobalOptionsModel::~GlobalOptionsModel() {
}

QString GlobalOptionsModel::separator() const {
return "/";
}

GlobalOptionsModel::TreeNode GlobalOptionsModel::findNode(const QString &name, const TreeNode &fnode) {
foreach (const TreeNode &node, fnode.children) {
if(node.name == name) {
return node;
}
}
return TreeNode();
};

QStringList GlobalOptionsModel::pathEntryList(const QString &path) {
QStringList files;

if(path == "/") {
foreach (const TreeNode &node, rootInfo.children) {
files << node.name;
}
return files;
} else {
QStringList pathList = path.split(separator());
TreeNode node = rootInfo;
for(int i = 1; i < pathList.size(); i++) {
node = findNode(pathList[i], node);
if(node.name.isEmpty()) {
return files;
}
}
foreach (const TreeNode &child, node.children) {
files << child.name;
}
}

return files;
}

void GlobalOptionsModel::pathInfo(QString path, bool &isDir, uint64_t &size, QDateTime &lastModified) {
isDir = false;
size = 0;
Q_UNUSED(lastModified);

if(path == "/") {
size = rootInfo.children.size();
if(size)
isDir = true;
} else {
QStringList pathList = path.split(separator());
TreeNode node = rootInfo;
for(int i = 1; i < pathList.size(); i++) {
node = findNode(pathList[i], node);
if(node.name.isEmpty()) {
return;
}
}
size = node.children.size();
if(size)
isDir = true;
}
}

void GlobalOptionsModel::setTree(const TreeNode &tree) {
rootInfo = tree;
}

const QString GlobalOptionsWindow::defaultColorScheme = "QuardCRT";
const QString GlobalOptionsWindow::defaultColorSchemeBak = "QuardCRT Light";

Expand All @@ -61,10 +133,11 @@ GlobalOptionsWindow::GlobalOptionsWindow(QWidget *parent) :
QSplitter *splitter = new QSplitter(Qt::Horizontal, this);
splitter->setHandleWidth(1);
ui->horizontalLayout->addWidget(splitter);
QTreeView *treeView = new QTreeView(this);
treeView = new QTreeView(this);
treeView->setHeaderHidden(true);
model = new QStringListModel(treeView);
treeView->setModel(model);
model = new GlobalOptionsModel(treeView);
model->setOnlyName(true);
model->setDistinguishType(false);
treeView->setEditTriggers(QAbstractItemView::NoEditTriggers);
splitter->addWidget(treeView);
QWidget *widget = new QWidget(this);
Expand All @@ -88,9 +161,9 @@ GlobalOptionsWindow::GlobalOptionsWindow(QWidget *parent) :
globalOptionsAdvancedWidget = new GlobalOptionsAdvancedWidget(widget);
widget->layout()->addWidget(globalOptionsAdvancedWidget);

setActiveWidget(0);

treeView->setModel(model);
retranslateUi();
setActiveWidget(globalOptionsGeneralWidget);

GlobalSetting settings;
globalOptionsAdvancedWidget->ui->lineEditConfigFile->setText(settings.fileName());
Expand Down Expand Up @@ -373,7 +446,17 @@ GlobalOptionsWindow::GlobalOptionsWindow(QWidget *parent) :
connect(ui->buttonBox, &QDialogButtonBox::rejected, this, &GlobalOptionsWindow::buttonBoxRejected);

connect(treeView, &QTreeView::clicked, [&](const QModelIndex &index) {
setActiveWidget(index.row());
QString filePath = model->filePath(index);
QStringList pathList = filePath.split(model->separator());
GlobalOptionsModel::TreeNode node = rootInfo;
for(int i = 1; i < pathList.size(); i++) {
node = GlobalOptionsModel::findNode(pathList[i], node);
if(node.name.isEmpty()) {
setActiveWidget(nullptr);
return;
}
}
setActiveWidget(node.widget);
});
}

Expand All @@ -384,7 +467,17 @@ GlobalOptionsWindow::~GlobalOptionsWindow()

void GlobalOptionsWindow::retranslateUi()
{
model->setStringList(QStringList() << tr("General") << tr("Appearance") << tr("Terminal") << tr("Window") << tr("Transfer") << tr("Advanced"));
rootInfo.children.clear();
GlobalOptionsModel::TreeNode generalNode(tr("General"),globalOptionsGeneralWidget);
GlobalOptionsModel::TreeNode appearance(tr("Appearance"),globalOptionsAppearanceWidget);
GlobalOptionsModel::TreeNode terminal(tr("Terminal"),globalOptionsTerminalWidget);
GlobalOptionsModel::TreeNode window(tr("Window"),globalOptionsWindowWidget);
GlobalOptionsModel::TreeNode transfer(tr("Transfer"),globalOptionsTransferWidget);
//transfer.children << GlobalOptionsModel::TreeNode(tr("Serial")) << GlobalOptionsModel::TreeNode("SSH");
GlobalOptionsModel::TreeNode advanced(tr("Advanced"),globalOptionsAdvancedWidget);
rootInfo.children << generalNode << appearance << terminal << window << transfer << advanced;
model->setTree(rootInfo);
treeView->setRootIndex(model->setRootPath("/"));
ui->retranslateUi(this);
globalOptionsGeneralWidget->ui->retranslateUi(this);
globalOptionsGeneralWidget->ui->retranslateUi(this);
Expand All @@ -395,7 +488,7 @@ void GlobalOptionsWindow::retranslateUi()
globalOptionsAdvancedWidget->ui->retranslateUi(this);
}

void GlobalOptionsWindow::setActiveWidget(int index)
void GlobalOptionsWindow::setActiveWidget(QWidget *widget)
{
globalOptionsGeneralWidget->setVisible(false);
globalOptionsAppearanceWidget->setVisible(false);
Expand All @@ -404,26 +497,8 @@ void GlobalOptionsWindow::setActiveWidget(int index)
globalOptionsTransferWidget->setVisible(false);
globalOptionsAdvancedWidget->setVisible(false);

switch(index) {
case 0:
globalOptionsGeneralWidget->setVisible(true);
break;
case 1:
globalOptionsAppearanceWidget->setVisible(true);
break;
case 2:
globalOptionsTerminalWidget->setVisible(true);
break;
case 3:
globalOptionsWindowWidget->setVisible(true);
break;
case 4:
globalOptionsTransferWidget->setVisible(true);
break;
case 5:
globalOptionsAdvancedWidget->setVisible(true);
break;
}
if(widget)
widget->setVisible(true);
}

void GlobalOptionsWindow::setAvailableColorSchemes(QStringList colorSchemes)
Expand Down
Loading

0 comments on commit 42a6bab

Please sign in to comment.