| 1 |
/* Copyright (c) 2020, Dyssol Development Team. |
| 2 |
* Copyright (c) 2024, DyssolTEC GmbH. |
| 3 |
* All rights reserved. This file is part of Dyssol. See LICENSE file for license information. */ |
| 4 |
|
| 5 |
#include "QtList.h" |
| 6 |
#include <QFocusEvent> |
| 7 |
|
| 8 |
CQtList::CQtList(QWidget* _parent) |
| 9 |
: QListWidget(_parent) |
| 10 |
{ |
| 11 |
} |
| 12 |
|
| 13 |
void CQtList::AddItemEditable(const std::string& _text, const QVariant& _userData) |
| 14 |
{ |
| 15 |
auto* item = new QListWidgetItem{ QString::fromStdString(_text), this }; |
| 16 |
if (_userData != -1) item->setData(Qt::UserRole, _userData); |
| 17 |
item->setFlags(item->flags() | Qt::ItemIsEditable); |
| 18 |
addItem(item); |
| 19 |
} |
| 20 |
|
| 21 |
void CQtList::InsertItemCheckable(int _row, const QString& _text, bool _checked, const QVariant& _userData) |
| 22 |
{ |
| 23 |
const bool exist = this->item(_row) != nullptr; |
| 24 |
auto* item = exist ? this->item(_row) : new QListWidgetItem(_text); |
| 25 |
if (exist) item->setText(_text); |
| 26 |
if (_userData != -1) item->setData(Qt::UserRole, _userData); |
| 27 |
item->setFlags(item->flags() | Qt::ItemIsUserCheckable); |
| 28 |
item->setCheckState(_checked ? Qt::Checked : Qt::Unchecked); |
| 29 |
if (!exist) |
| 30 |
insertItem(_row, item); |
| 31 |
} |
| 32 |
|
| 33 |
void CQtList::InsertItemCheckable(int _row, const std::string& _text, bool _checked, const QVariant& _userData) |
| 34 |
{ |
| 35 |
InsertItemCheckable(count(), QString::fromStdString(_text), _checked, _userData); |
| 36 |
} |
| 37 |
|
| 38 |
void CQtList::AddItemCheckable(const QString& _text, bool _checked, const QVariant& _userData) |
| 39 |
{ |
| 40 |
InsertItemCheckable(count(), _text, _checked, _userData); |
| 41 |
} |
| 42 |
|
| 43 |
void CQtList::SetItemChecked(int _row, bool _checked) const |
| 44 |
{ |
| 45 |
if (item(_row)) |
| 46 |
item(_row)->setCheckState(_checked ? Qt::Checked : Qt::Unchecked); |
| 47 |
} |
| 48 |
|
| 49 |
bool CQtList::GetItemChecked(int _row) const |
| 50 |
{ |
| 51 |
if (item(_row)) |
| 52 |
return item(_row)->checkState() == Qt::Checked; |
| 53 |
return false; |
| 54 |
} |
| 55 |
|
| 56 |
std::string CQtList::CurrentItemUserDataStr() const |
| 57 |
{ |
| 58 |
if (!currentItem()) return {}; |
| 59 |
return currentItem()->data(Qt::UserRole).toString().toStdString(); |
| 60 |
} |
| 61 |
|
| 62 |
size_t CQtList::CurrentItemUserDataUInt() const |
| 63 |
{ |
| 64 |
if (!currentItem()) return -1; |
| 65 |
return currentItem()->data(Qt::UserRole).toString().toULongLong(); |
| 66 |
} |
| 67 |
|
| 68 |
void CQtList::RestoreCurrentRow(int _row) |
| 69 |
{ |
| 70 |
if (_row < count()) setCurrentRow(_row); |
| 71 |
else if (_row != -1 && count() != 0) setCurrentRow(count() - 1); |
| 72 |
else if (count() != 0) setCurrentRow(0); |
| 73 |
else setCurrentRow(-1); |
| 74 |
} |
| 75 |
|
| 76 |
void CQtList::focusInEvent(QFocusEvent* _e) |
| 77 |
{ |
| 78 |
if (_e->gotFocus()) |
| 79 |
emit GotFocus(); |
| 80 |
QListWidget::focusInEvent(_e); |
| 81 |
} |
| 82 |
|
| 83 |
void CQtList::focusOutEvent(QFocusEvent* _e) |
| 84 |
{ |
| 85 |
if (_e->lostFocus()) |
| 86 |
emit LostFocus(); |
| 87 |
QListWidget::focusOutEvent(_e); |
| 88 |
} |