Skip to content

Commit

Permalink
Editor: Fixed abnormal duplicates problem
Browse files Browse the repository at this point in the history
At the Layers list, and other places where QListWidget is used: the reason that findItems way never guarantees the giving of all the content. Instead, let's use the count() and item(i) way that will work 100%.
  • Loading branch information
Wohlstand committed Dec 12, 2024
1 parent 359c413 commit 6ba2ce5
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 20 deletions.
40 changes: 37 additions & 3 deletions Editor/common_features/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#include <QDialog>
#include <QApplication>
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
#include <QDesktopWidget>
# include <QDesktopWidget>
#endif
#include <QScreen>
#include <QStyle>
Expand All @@ -30,6 +30,9 @@
#include <QTableWidget>
#include <QComboBox>
#include <QTabBar>
#ifdef DEBUG_BUILD
# include <QtDebug>
#endif
#include <pge_qt_compat.h>

#include "util.h"
Expand Down Expand Up @@ -82,26 +85,57 @@ void util::updateFilter(QLineEdit *searchEdit, QListWidget *itemList, int search
itemList->update();
}

QList<QListWidgetItem *> util::items(QListWidget *wid)
{
QList<QListWidgetItem *> items;

for(int i = 0; i < wid->count(); ++i)
{
auto *it = wid->item(i);
if(it)
items.push_back(it);
}

return items;
}

void util::memclear(QListWidget *wid)
{
QList<QListWidgetItem *> items = wid->findItems(QString("*"), Qt::MatchWrap | Qt::MatchWildcard);
QList<QListWidgetItem *> items = util::items(wid);
while(!items.isEmpty())
{
QListWidgetItem *tmp = items.first();
items.pop_front();
#ifdef DEBUG_BUILD
qDebug() << "QListWidget: Popping item with label" << tmp->text();
#endif
delete tmp;
}

wid->clear();
}

void util::memclear(QTableWidget *wid)
{
QList<QTableWidgetItem *> items = wid->findItems(QString("*"), Qt::MatchWrap | Qt::MatchWildcard);
QList<QTableWidgetItem *> items;
for(int x = 0; x < wid->rowCount(); ++x)
{
for(int y = 0; y < wid->columnCount(); ++y)
{
auto *it = wid->item(x, y);
if(it)
items.push_back(wid->item(x, y));
}
}

while(!items.isEmpty())
{
QTableWidgetItem *tmp = items.first();
items.pop_front();
delete tmp;
}

wid->clear();
}

void util::clearLayoutItems(QLayout *layout)
Expand Down
6 changes: 6 additions & 0 deletions Editor/common_features/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class QDialog;
class QLayout;
class QLineEdit;
class QListWidget;
class QListWidgetItem;
class QTableWidget;
class QComboBox;
class QTabBar;
Expand All @@ -41,6 +42,11 @@ class util
* \param typeBox ComboBox contains type of search (by contained string in a name or ID number of the element)
*/
static void updateFilter(QLineEdit* searchEdit, QListWidget* itemList, int searchType);
/*!
* \brief Returns a list of all items of QListWidget
* \param wid Pointer to QListWidget
*/
static QList<QListWidgetItem *> items(QListWidget* wid);
/*!
* \brief Removes all entries of QListWidget (with deletion!)
* \param wid Pointer to QListWidget
Expand Down
4 changes: 2 additions & 2 deletions Editor/data_configs/selection_dialog/config_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ void ConfigManager::saveCurrentSettings()
bool ConfigManager::hasConfigPacks()
{
//Warning message: if no installed config packs
if(ui->configList->findItems(QString("*"), Qt::MatchWrap | Qt::MatchWildcard).isEmpty())
if(ui->configList->count() == 0)
{
QMessageBox msgBox(this);
msgBox.setWindowTitle(tr("No config packs were found"));
Expand Down Expand Up @@ -484,7 +484,7 @@ QString ConfigManager::loadConfigs()
if(!saved_theme.isEmpty())
m_themePackName = saved_theme;

auto availableConfigs = ui->configList->findItems(QString("*"), Qt::MatchWrap | Qt::MatchWildcard);
auto availableConfigs = util::items(ui->configList);

// Automatically choice a config pack if only one detected
if(availableConfigs.size() == 1)
Expand Down
4 changes: 3 additions & 1 deletion Editor/main_window/dock/debugger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,9 @@ void DebuggerBox::on_DEBUG_CustomCountersList_itemClicked(QListWidgetItem *item)
Q_UNUSED(item);
int itemID = item->data(Qt::UserRole).toInt();
on_DEBUG_RefreshCoutners_clicked();
foreach(QListWidgetItem *x, ui->DEBUG_CustomCountersList->findItems("*", Qt::MatchWildcard))

auto items = util::items(ui->DEBUG_CustomCountersList);
for(QListWidgetItem *x : items)
{
if(x->data(Qt::UserRole).toInt() == itemID)
{
Expand Down
15 changes: 6 additions & 9 deletions Editor/main_window/dock/lvl_events_box.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -732,10 +732,9 @@ void LvlEventsBox::eventLayerVisiblySyncList()
item = new QListWidgetItem;
item->setText(layer);
item->setFlags(item->flags() | Qt::ItemIsEnabled | Qt::ItemIsSelectable);
QList<QListWidgetItem *> items =
ui->LVLEvents_layerList->findItems(QString("*"), Qt::MatchWrap | Qt::MatchWildcard);

foreach(QListWidgetItem *item, items)
QList<QListWidgetItem *> items = util::items(ui->LVLEvents_layerList);
for(QListWidgetItem *item : items)
{
if(item->text() == layer)
{
Expand All @@ -753,10 +752,9 @@ void LvlEventsBox::eventLayerVisiblySyncList()
item = new QListWidgetItem;
item->setText(layer);
item->setFlags(item->flags() | Qt::ItemIsEnabled | Qt::ItemIsSelectable);
QList<QListWidgetItem *> items =
ui->LVLEvents_layerList->findItems(QString("*"), Qt::MatchWrap | Qt::MatchWildcard);

foreach(QListWidgetItem *item, items)
QList<QListWidgetItem *> items = util::items(ui->LVLEvents_layerList);
for(QListWidgetItem *item : items)
{
if(item->text() == layer)
{
Expand All @@ -774,10 +772,9 @@ void LvlEventsBox::eventLayerVisiblySyncList()
item = new QListWidgetItem;
item->setText(layer);
item->setFlags(item->flags() | Qt::ItemIsEnabled | Qt::ItemIsSelectable);
QList<QListWidgetItem *> items =
ui->LVLEvents_layerList->findItems(QString("*"), Qt::MatchWrap | Qt::MatchWildcard);

foreach(QListWidgetItem *item, items)
QList<QListWidgetItem *> items = util::items(ui->LVLEvents_layerList);
for(QListWidgetItem *item : items)
{
if(item->text() == layer)
{
Expand Down
8 changes: 7 additions & 1 deletion Editor/main_window/dock/lvl_layers_box.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,10 @@ void LvlLayersBox::setLayersBox()

if(WinType == MainWindow::WND_Level)
{
foreach(LevelLayer layer, edit->LvlData.layers)
#ifdef DEBUG_BUILD
qDebug() << "Filling the layers list widget by items";
#endif

for(LevelLayer &layer : edit->LvlData.layers)
{
item = new QListWidgetItem();
Expand All @@ -131,6 +134,9 @@ void LvlLayersBox::setLayersBox()
item->setCheckState((layer.hidden) ? Qt::Unchecked : Qt::Checked);
item->setData(Qt::UserRole, QString::number(layer.meta.array_id));
ui->LvlLayerList->addItem(item);
#ifdef DEBUG_BUILD
qDebug() << "Added layer item" << layer.name << "into the widget...";
#endif
}
}
}
Expand Down
10 changes: 6 additions & 4 deletions Editor/tools/debugger/custom_counter_gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,9 @@ void CustomCounterGUI::replaceItem(long what, long with)
if(counterData.items.contains(what))
{
counterData.items[counterData.items.indexOf(what)]=with;
QList<QListWidgetItem * > items = ui->ItemList->findItems("*", Qt::MatchWildcard);
foreach(QListWidgetItem* x, items)

QList<QListWidgetItem * > items = util::items(ui->ItemList);
for(QListWidgetItem* x : items)
{
if(x->data(Qt::UserRole).toInt()==what)
{
Expand All @@ -126,8 +127,9 @@ void CustomCounterGUI::removeItem(long what)
if(counterData.items.contains(what))
{
counterData.items.remove(counterData.items.indexOf(what));
QList<QListWidgetItem * > items = ui->ItemList->findItems("*", Qt::MatchWildcard);
foreach(QListWidgetItem* x, items)

QList<QListWidgetItem * > items = util::items(ui->ItemList);
for(QListWidgetItem* x : items)
{
if(x->data(Qt::UserRole).toInt()==what)
{
Expand Down
1 change: 1 addition & 0 deletions changelog.editor.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ beta2
- Reworked the message box edit dialogue: Added the accurate preview for different target engines
- Added layer and event statistics that can be opened by layer's or event's context menu
- Fixed random crashes when removing objects from the scene
- Resolved the problem of abnormal duplicates of layers and events entries appear in the GUI lists

Editor 0.3.2
- Added support for an element (Block, BGO, NPC), section, and level-wide Extra Settings
Expand Down

0 comments on commit 6ba2ce5

Please sign in to comment.