Skip to content

Commit d916fab

Browse files
Added ability to remove windows from lists.
- Button for removing selected windows from the active/inactive lists has been added. - Shortcut for doing the same has also been added, using the DEL key. - Removed Q_UNUSED from nativeEvent, as the variables that were marked unused were actually being used in the call to QMainWindow::nativeEvent. - Made destructor of ListWidgetWindowItem call ApplyOriginalState(), so that when ListWidgetWindowItem's are deleted from the active/inactive lists by the aforementioned shortcut and buttons, the original window state will be restored. - Updated the way in which invalid windows are periodically deleted from the active/inactive lists, ensuring that QListWidget::takeItem is called first before the item instance is deleted, as recommended by Qt. - Added new function: selectedWindows_Delete() which the aforementioned button and shortcut call to delete selected windows. - Re-ordered includes in main_window_dialog.hxx, aesthetic change only. - Changed default window height of MainWindow from 491 to 482.
1 parent 0110bd4 commit d916fab

5 files changed

+72
-27
lines changed

source/list_widget_window_item.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -178,3 +178,7 @@ ListWidgetWindowItem::ListWidgetWindowItem(const HWND& window_handle)
178178
{
179179

180180
}
181+
182+
ListWidgetWindowItem::~ListWidgetWindowItem() {
183+
ApplyOriginalState();
184+
}

source/list_widget_window_item.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ class ListWidgetWindowItem : public QListWidgetItem {
7070
void ResetModifications();
7171

7272
ListWidgetWindowItem(const HWND& window_handle);
73+
virtual ~ListWidgetWindowItem() override;
7374
};
7475

7576
#endif // ListWidgetWindowItem_HXX

source/main_window_dialog.cxx

+30-5
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22
#include "ui_main_window_dialog.h"
33

44
bool MainWindow::nativeEvent(const QByteArray& event_type, void* message, qintptr* result) {
5-
Q_UNUSED(event_type)
6-
Q_UNUSED(result)
7-
85
MSG* msg { reinterpret_cast<MSG*>(message) };
96

107
if (msg->message == WM_HOTKEY) {
@@ -22,15 +19,15 @@ void MainWindow::removeInvalidWindowsFromLists() {
2219
ListWidgetWindowItem* list_widget_window_item { dynamic_cast<ListWidgetWindowItem*>(ui->listWidgetActiveWindows->item(i)) };
2320

2421
if(!list_widget_window_item->IsValidWindow()) {
25-
delete list_widget_window_item;
22+
delete ui->listWidgetActiveWindows->takeItem(ui->listWidgetActiveWindows->row(list_widget_window_item));
2623
}
2724
}
2825

2926
for(qint32 i { 0 }; i < ui->listWidgetInactiveWindows->count(); ++i) {
3027
ListWidgetWindowItem* list_widget_window_item { dynamic_cast<ListWidgetWindowItem*>(ui->listWidgetInactiveWindows->item(i)) };
3128

3229
if(!list_widget_window_item->IsValidWindow()) {
33-
delete list_widget_window_item;
30+
delete ui->listWidgetInactiveWindows->takeItem(ui->listWidgetInactiveWindows->row(list_widget_window_item));
3431
}
3532
}
3633
}
@@ -95,6 +92,16 @@ void MainWindow::spawnProcessScannerDialog() {
9592
}
9693
}
9794

95+
void MainWindow::selectedWindows_Delete() {
96+
for(QListWidgetItem* list_widget_item : ui->listWidgetInactiveWindows->selectedItems()) {
97+
delete ui->listWidgetInactiveWindows->takeItem(ui->listWidgetInactiveWindows->row(list_widget_item));
98+
}
99+
100+
for(QListWidgetItem* list_widget_item : ui->listWidgetActiveWindows->selectedItems()) {
101+
delete ui->listWidgetActiveWindows->takeItem(ui->listWidgetActiveWindows->row(list_widget_item));
102+
}
103+
}
104+
98105
void MainWindow::startWindowGrabber() {
99106
if(timerWindowGrabber->isActive()) {
100107
windowGrabAttemptCounter = 30;
@@ -327,6 +334,24 @@ MainWindow::MainWindow(QWidget* parent)
327334

328335
timerRemoveInvalidWindows->start(1000);
329336

337+
QShortcut* inactive_list_widget_del_shortcut { new QShortcut { Qt::Key_Delete, ui->listWidgetInactiveWindows } };
338+
QShortcut* active_list_widget_del_shortcut { new QShortcut { Qt::Key_Delete, ui->listWidgetActiveWindows } };
339+
340+
connect(inactive_list_widget_del_shortcut, SIGNAL(activated()),
341+
this, SLOT(selectedWindows_Delete()));
342+
343+
connect(active_list_widget_del_shortcut, SIGNAL(activated()),
344+
this, SLOT(selectedWindows_Delete()));
345+
346+
connect(inactive_list_widget_del_shortcut, SIGNAL(activatedAmbiguously()),
347+
this, SLOT(selectedWindows_Delete()));
348+
349+
connect(active_list_widget_del_shortcut, SIGNAL(activatedAmbiguously()),
350+
this, SLOT(selectedWindows_Delete()));
351+
352+
connect(ui->pushButtonDeleteSelectedWindows, SIGNAL(clicked()),
353+
this, SLOT(selectedWindows_Delete()));
354+
330355
connect(timerRemoveInvalidWindows, SIGNAL(timeout()),
331356
this, SLOT(removeInvalidWindowsFromLists()));
332357

source/main_window_dialog.hxx

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
#ifndef MAINWINDOW_HXX
22
#define MAINWINDOW_HXX
33

4-
#include <QtWidgets/QMainWindow>
5-
#include <QtWidgets/QHBoxLayout>
6-
#include <QtWidgets/QCheckBox>
7-
84
#include <QtCore/QResource>
95
#include <QtCore/QFileInfo>
106
#include <QtCore/QFile>
117

8+
#include <QtGui/QShortcut>
9+
10+
#include <QtWidgets/QMainWindow>
11+
#include <QtWidgets/QHBoxLayout>
12+
#include <QtWidgets/QCheckBox>
13+
1214
#ifndef WIN32_MEAN_AND_LEAN
1315
#define WIN32_MEAN_AND_LEAN
1416
#endif
@@ -61,6 +63,8 @@ protected:
6163
quint32 windowGrabAttemptCounter;
6264
Q_SLOT void startWindowGrabber();
6365

66+
Q_SLOT void selectedWindows_Delete();
67+
6468
Q_SLOT void selectedInactiveWindows_Activate();
6569
Q_SLOT void selectedActiveWindows_Deactivate();
6670

source/main_window_dialog.ui

+29-18
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<x>0</x>
88
<y>0</y>
99
<width>457</width>
10-
<height>491</height>
10+
<height>482</height>
1111
</rect>
1212
</property>
1313
<property name="sizePolicy">
@@ -212,23 +212,34 @@
212212
</widget>
213213
</item>
214214
<item>
215-
<widget class="QPushButton" name="pushButtonResetSelectedWindows">
216-
<property name="sizePolicy">
217-
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
218-
<horstretch>0</horstretch>
219-
<verstretch>0</verstretch>
220-
</sizepolicy>
221-
</property>
222-
<property name="minimumSize">
223-
<size>
224-
<width>0</width>
225-
<height>0</height>
226-
</size>
227-
</property>
228-
<property name="text">
229-
<string>Reset Selected Windows</string>
230-
</property>
231-
</widget>
215+
<layout class="QHBoxLayout" name="horizontalLayoutListManagementButtons" stretch="1,1">
216+
<item>
217+
<widget class="QPushButton" name="pushButtonResetSelectedWindows">
218+
<property name="sizePolicy">
219+
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
220+
<horstretch>0</horstretch>
221+
<verstretch>0</verstretch>
222+
</sizepolicy>
223+
</property>
224+
<property name="minimumSize">
225+
<size>
226+
<width>0</width>
227+
<height>0</height>
228+
</size>
229+
</property>
230+
<property name="text">
231+
<string>Reset Selected Windows</string>
232+
</property>
233+
</widget>
234+
</item>
235+
<item>
236+
<widget class="QPushButton" name="pushButtonDeleteSelectedWindows">
237+
<property name="text">
238+
<string>Delete Selected Windows</string>
239+
</property>
240+
</widget>
241+
</item>
242+
</layout>
232243
</item>
233244
</layout>
234245
</widget>

0 commit comments

Comments
 (0)