| 1 |
#include "widgets/list.h" |
| 2 |
|
| 3 |
List::List(QString title) |
| 4 |
{ |
| 5 |
layout = new QVBoxLayout; |
| 6 |
this->setLayout(layout); |
| 7 |
|
| 8 |
headerLayout = new QHBoxLayout; |
| 9 |
headerLayout->setAlignment(Qt::AlignTop); |
| 10 |
DLabel *titleLabel = new DLabel(title); |
| 11 |
QFont titleFont; |
| 12 |
titleFont.setPixelSize(28); |
| 13 |
titleLabel->setFont(titleFont); |
| 14 |
headerLayout->addWidget(titleLabel); |
| 15 |
headerLayout->addStretch(); |
| 16 |
headerLayout->setSpacing(10); |
| 17 |
headerLayout->setMargin(10); |
| 18 |
layout->addLayout(headerLayout); |
| 19 |
|
| 20 |
listView = new DListView; |
| 21 |
listView->setViewMode(QListView::IconMode); |
| 22 |
listView->setItemSize(QSize(128,128)); |
| 23 |
listView->setIconSize(QSize(64, 64)); |
| 24 |
listView->setSpacing(20); |
| 25 |
listView->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); |
| 26 |
listView->setSelectionMode(QAbstractItemView::NoSelection); |
| 27 |
listView->setMaximumWidth(QWIDGETSIZE_MAX); |
| 28 |
listView->setMinimumWidth(188); |
| 29 |
listView->setEditTriggers(QListView::NoEditTriggers); |
| 30 |
listView->setResizeMode(QListView::Adjust); |
| 31 |
listView->setOrientation(QListView::LeftToRight, true); |
| 32 |
listView->setFocusPolicy(Qt::NoFocus); |
| 33 |
DStyle::setFrameRadius(listView, 18); |
| 34 |
model = new QStandardItemModel; |
| 35 |
listView->setModel(model); |
| 36 |
|
| 37 |
connect(listView, qOverload<const QModelIndex &>(&DListView::currentChanged), this, [ = ] { |
| 38 |
if (listView->currentIndex().row() != -1) { |
| 39 |
emit currentItemChanged(model->item(listView->currentIndex().row())->data()); |
| 40 |
listView->setCurrentIndex(QModelIndex()); |
| 41 |
} |
| 42 |
}); |
| 43 |
|
| 44 |
loading = new DSpinner; |
| 45 |
loading->setFixedSize(50, 50); |
| 46 |
loading->start(); |
| 47 |
layout->addWidget(loading, 0, Qt::AlignHCenter | Qt::AlignTop); |
| 48 |
|
| 49 |
emptyLabel = new DLabel; |
| 50 |
QFont emptyFont; |
| 51 |
emptyFont.setPixelSize(28); |
| 52 |
emptyLabel->setFont(emptyFont); |
| 53 |
emptyLabel->setAlignment(Qt::AlignHCenter); |
| 54 |
emptyLabel->hide(); |
| 55 |
layout->addWidget(emptyLabel, 0, Qt::AlignTop); |
| 56 |
} |
| 57 |
|
| 58 |
void List::addItem(QString name, QIcon icon, QVariant data) |
| 59 |
{ |
| 60 |
DStandardItem *item = new DStandardItem(icon, name); |
| 61 |
item->setData(data.isNull() ? name : data); |
| 62 |
model->appendRow(item); |
| 63 |
} |
| 64 |
|
| 65 |
void List::addHeaderWidget(QWidget *widget) |
| 66 |
{ |
| 67 |
headerLayout->addWidget(widget); |
| 68 |
} |
| 69 |
|
| 70 |
void List::setEmptyText(QString text) |
| 71 |
{ |
| 72 |
emptyLabel->setText(text); |
| 73 |
} |
| 74 |
|
| 75 |
void List::editItemText(QString package, QString text) |
| 76 |
{ |
| 77 |
for (int i = 0; i < model->rowCount(); i++) { |
| 78 |
if (model->item(i)->data() == package) { |
| 79 |
model->item(i)->setText(text); |
| 80 |
} |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
void List::clear() |
| 85 |
{ |
| 86 |
model->clear(); |
| 87 |
listView->removeItems(0, listView->count()); |
| 88 |
} |
| 89 |
|
| 90 |
void List::load() |
| 91 |
{ |
| 92 |
loading->hide(); |
| 93 |
if (model->rowCount() == 0) { |
| 94 |
layout->removeWidget(listView); |
| 95 |
emptyLabel->show(); |
| 96 |
} else { |
| 97 |
layout->addWidget(listView); |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
void List::unload() |
| 102 |
{ |
| 103 |
loading->show(); |
| 104 |
emptyLabel->hide(); |
| 105 |
layout->removeWidget(listView); |
| 106 |
} |