Skip to content

Commit 161e0c3

Browse files
authored
Merge pull request #8 from IlanVinogard/feature/add-new-function-and-comments
This pull request refactors the MiniThingQt class to enhance code readability and maintainability.
2 parents 33d6ae9 + 129f868 commit 161e0c3

File tree

1 file changed

+49
-53
lines changed

1 file changed

+49
-53
lines changed

MiniThing/Qt/MiniThingQt.cpp

+49-53
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,12 @@
88
// to the system's file path format before opening.
99
void MiniThingQt::OpenFilePath(const QString& filePath)
1010
{
11-
QProcess process;
12-
QString adjustedPath = filePath;
13-
adjustedPath.replace("/", "\\");
11+
QString adjustedPath = filePath.replace("/", "\\");
1412
QString command = QString("explorer.exe /select,\"%1\"").arg(adjustedPath);
15-
process.startDetached(command);
13+
QProcess::startDetached(command);
1614
}
1715

16+
1817
// Attempts to open a file at the specified path. Returns true if the file is successfully opened,
1918
// and false with an error message if the file cannot be opened.
2019
bool MiniThingQt::OpenFile(const QString& filePath)
@@ -32,84 +31,81 @@ MiniThingQt::MiniThingQt(QWidget* parent) : QMainWindow(parent)
3231
m_ui.setupUi(this);
3332
m_usnSet.clear();
3433

35-
// Setup logo icon
34+
// Setup UI components
35+
setupUIComponents();
36+
}
37+
38+
void MiniThingQt::setupUIComponents()
39+
{
40+
// Setup logo and other UI elements
3641
QIcon logo("Logo.ico");
3742
this->setWindowIcon(logo);
38-
39-
// Setup background image
40-
// QPixmap backgroundImg = QPixmap("./Background.png").scaled(this->size());
4143
QPalette palette;
42-
// palette.setBrush(QPalette::Window, QBrush(backgroundImg));
4344
palette.setColor(QPalette::Window, QColor(255, 255, 255));
4445
this->setPalette(palette);
4546

46-
// Setup status bar
47+
// Status Bar setup
4748
m_statusBar = new QLabel;
4849
statusBar()->addWidget(m_statusBar);
49-
statusBar()->setStyleSheet("QLabel{ color: black }");
50+
statusBar()->setStyleSheet("QLabel { color: black }");
5051

52+
// Setup actions, menus, and other signal-slot connections
53+
setupActionsAndMenus();
5154
m_pMiniThingCore = new MiniThingCore();
5255
m_pMiniThingQtWorkThread = new MiniThingQtWorkThread(m_pMiniThingCore, statusBar());
5356

5457
// Connect UpdateStatusBar(), so we could update status bar in diff thread
5558
connect(m_pMiniThingQtWorkThread, &MiniThingQtWorkThread::UpdateStatusBar, this, &MiniThingQt::UpdateStatusBar);
56-
5759
m_pMiniThingQtWorkThread->start();
5860

59-
// Monitor enter press in line edit
61+
// Setup table view
62+
m_ui.tableView->setSelectionBehavior(QAbstractItemView::SelectionBehavior::SelectRows);
63+
m_ui.tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
64+
m_ui.tableView->verticalHeader()->setVisible(true);
65+
// Uncomment the next lines if you need to set specific resize modes for headers
66+
// m_ui.tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
67+
// m_ui.tableView->horizontalHeader()->setSectionResizeMode(0, QHeaderView::ResizeToContents);
68+
// m_ui.tableView->verticalHeader()->setSectionResizeMode(QHeaderView::Stretch);
69+
m_ui.tableView->setContextMenuPolicy(Qt::CustomContextMenu);
70+
71+
// Connect line edit returnPressed signal to EnterPressDown slot
6072
connect(m_ui.lineEdit, SIGNAL(returnPressed()), this, SLOT(EnterPressDown()));
6173

62-
// Connect MiniThing github link button
74+
// Connect push button clicked signal to ButtonSearchClicked slot
6375
connect(m_ui.pushButton, SIGNAL(clicked()), this, SLOT(ButtonSearchClicked()));
6476

65-
// Setup right key
66-
m_rightKeyMenu = new QMenu(m_ui.tableView);
67-
m_rightKeyActionOpen = new QAction();
68-
m_rightKeyActionOpenPath = new QAction();
69-
70-
m_rightKeyActionOpen->setText(QString("Open file (ctrl+o)"));
71-
m_rightKeyActionOpenPath->setText(QString("Open file path (ctrl+p)"));
77+
// Setup table and update view
78+
UpdateTableView();
79+
}
7280

81+
oid MiniThingQt::setupActionsAndMenus()
82+
{
83+
// Setup right-click menu
84+
m_rightKeyMenu = new QMenu(m_ui.tableView);
85+
m_rightKeyActionOpen = new QAction(tr("Open file (ctrl+o)"), this);
86+
m_rightKeyActionOpenPath = new QAction(tr("Open file path (ctrl+p)"), this);
7387
m_rightKeyMenu->addAction(m_rightKeyActionOpen);
7488
m_rightKeyMenu->addAction(m_rightKeyActionOpenPath);
7589
connect(m_ui.tableView, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(RightKeyMenu(QPoint)));
7690

77-
// Registe all shortcut keys
78-
m_shortKeySearch = new QAction(this);
79-
m_shortKeySearch->setShortcut(tr("ctrl+f"));
80-
this->addAction(m_shortKeySearch);
81-
connect(m_shortKeySearch, SIGNAL(triggered()), this, SLOT(ShortKeySearch()));
91+
// Setup shortcut keys
92+
m_shortKeySearch = new QAction(tr("Search (ctrl+f)"), this);
93+
m_shortKeyOpen = new QAction(tr("Open (ctrl+o)"), this);
94+
m_shortKeyOpenPath = new QAction(tr("Open Path (ctrl+p)"), this);
95+
m_shortKeyDelete = new QAction(tr("Delete (ctrl+d)"), this);
96+
m_shortKeyCopy = new QAction(tr("Copy (ctrl+c)"), this);
8297

83-
m_shortKeyOpen = new QAction(this);
84-
m_shortKeyOpen->setShortcut(tr("ctrl+o"));
85-
this->addAction(m_shortKeyOpen);
86-
connect(m_shortKeyOpen, SIGNAL(triggered()), this, SLOT(ShortKeyOpen()));
98+
addAction(m_shortKeySearch);
99+
addAction(m_shortKeyOpen);
100+
addAction(m_shortKeyOpenPath);
101+
addAction(m_shortKeyDelete);
102+
addAction(m_shortKeyCopy);
87103

88-
m_shortKeyOpenPath = new QAction(this);
89-
m_shortKeyOpenPath->setShortcut(tr("ctrl+p"));
90-
this->addAction(m_shortKeyOpenPath);
104+
connect(m_shortKeySearch, SIGNAL(triggered()), this, SLOT(ShortKeySearch()));
105+
connect(m_shortKeyOpen, SIGNAL(triggered()), this, SLOT(ShortKeyOpen()));
91106
connect(m_shortKeyOpenPath, SIGNAL(triggered()), this, SLOT(ShortKeyOpenPath()));
92-
93-
m_shortKeyDelete = new QAction(this);
94-
m_shortKeyDelete->setShortcut(tr("ctrl+d"));
95-
this->addAction(m_shortKeyDelete);
96107
connect(m_shortKeyDelete, SIGNAL(triggered()), this, SLOT(ShortKeyDelete()));
97-
98-
m_shortKeyCopy = new QAction(this);
99-
m_shortKeyCopy->setShortcut(tr("ctrl+c"));
100-
this->addAction(m_shortKeyCopy);
101108
connect(m_shortKeyCopy, SIGNAL(triggered()), this, SLOT(ShortKeyCopy()));
102-
103-
// Setup table view
104-
m_ui.tableView->setSelectionBehavior(QAbstractItemView::SelectionBehavior::SelectRows);
105-
m_ui.tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
106-
m_ui.tableView->verticalHeader()->setVisible(true);
107-
// ui.tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
108-
// ui.tableView->horizontalHeader()->setSectionResizeMode(0, QHeaderView::ResizeToContents);
109-
// ui.tableView->verticalHeader()->setSectionResizeMode(QHeaderView::Stretch);
110-
m_ui.tableView->setContextMenuPolicy(Qt::CustomContextMenu);
111-
112-
UpdateTableView();
113109
}
114110

115111
MiniThingQt::~MiniThingQt()
@@ -358,4 +354,4 @@ void MiniThingQt::RightKeyMenu(QPoint pos)
358354
this->SetStatusBar(tr("File opened"));
359355
}
360356
}
361-
}
357+
}

0 commit comments

Comments
 (0)