void tst_QStandardItem::sortChildren() { for (int x = 0; x < 2; ++x) { QStandardItemModel *model = new QStandardItemModel; QStandardItem *item = (x == 0) ? new QStandardItem : model->invisibleRootItem(); QStandardItem *one = new QStandardItem; one->appendRow(new QStandardItem(QLatin1String("a"))); one->appendRow(new QStandardItem(QLatin1String("b"))); one->appendRow(new QStandardItem(QLatin1String("c"))); QStandardItem *two = new QStandardItem; two->appendRow(new QStandardItem(QLatin1String("f"))); two->appendRow(new QStandardItem(QLatin1String("d"))); two->appendRow(new QStandardItem(QLatin1String("e"))); item->appendRow(one); item->appendRow(two); QSignalSpy layoutAboutToBeChangedSpy( model, SIGNAL(layoutAboutToBeChanged())); QSignalSpy layoutChangedSpy( model, SIGNAL(layoutChanged())); one->sortChildren(0, Qt::DescendingOrder); // verify sorted QCOMPARE(one->child(0)->text(), QLatin1String("c")); QCOMPARE(one->child(1)->text(), QLatin1String("b")); QCOMPARE(one->child(2)->text(), QLatin1String("a")); // verify siblings unaffected QCOMPARE(two->child(0)->text(), QLatin1String("f")); QCOMPARE(two->child(1)->text(), QLatin1String("d")); QCOMPARE(two->child(2)->text(), QLatin1String("e")); two->sortChildren(0, Qt::AscendingOrder); // verify sorted QCOMPARE(two->child(0)->text(), QLatin1String("d")); QCOMPARE(two->child(1)->text(), QLatin1String("e")); QCOMPARE(two->child(2)->text(), QLatin1String("f")); // verify siblings unaffected QCOMPARE(one->child(0)->text(), QLatin1String("c")); QCOMPARE(one->child(1)->text(), QLatin1String("b")); QCOMPARE(one->child(2)->text(), QLatin1String("a")); item->sortChildren(0, Qt::AscendingOrder); // verify everything sorted QCOMPARE(one->child(0)->text(), QLatin1String("a")); QCOMPARE(one->child(1)->text(), QLatin1String("b")); QCOMPARE(one->child(2)->text(), QLatin1String("c")); QCOMPARE(two->child(0)->text(), QLatin1String("d")); QCOMPARE(two->child(1)->text(), QLatin1String("e")); QCOMPARE(two->child(2)->text(), QLatin1String("f")); QCOMPARE(layoutAboutToBeChangedSpy.count(), (x == 0) ? 0 : 3); QCOMPARE(layoutChangedSpy.count(), (x == 0) ? 0 : 3); if (x == 0) delete item; delete model; } }
void resetModel() { q->clear(); QFont bold; bold.setBold(true); // Create one item for each day of week for(int day = Qt::Monday; day <= Qt::Sunday; ++day) { QStandardItem *dayItem = new QStandardItem(QDate::longDayName(day)); dayItem->setFont(bold); dayItem->setData(day, WeekDayRole); // Add availabilities to items const QVector<DayAvailability> &avail = m_UserCalendar->availabilities(day); for(int availabilityId = 0; availabilityId < avail.count(); ++availabilityId) { for(int timeRangeId = 0; timeRangeId < avail.at(availabilityId).timeRangeCount(); ++timeRangeId) { TimeRange range = avail.at(availabilityId).timeRangeAt(timeRangeId); QStandardItem *time = new QStandardItem(tkTr(Trans::Constants::FROM_1_TO_2).arg(range.from.toString()).arg(range.to.toString())); time->setData(day, WeekDayRole); time->setData(range.from, HourFromRole); time->setData(range.to, HourToRole); time->setData(timeRangeId, TimeRangeIdRole); time->setData(availabilityId, AvailIdRole); time->setToolTip(time->text()); dayItem->appendRow(time); } } if (dayItem->rowCount() > 0) { dayItem->sortChildren(0); } q->invisibleRootItem()->appendRow(dayItem); } }
// private slot void FilesWidget::addDocument(Document *document) { Q_ASSERT(document != NULL); Q_ASSERT(document != DocumentManager::current()); const Location &location = document->location(); const QString &filePath = location.filePath("unnamed"); const QString &directoryPath = location.directoryPath("unnamed"); const QString &fileName = location.fileName("unnamed"); // Find or create parent item const QModelIndexList &parents = m_model.match(m_model.index(0, 0, QModelIndex()), DirectoryPathRole, directoryPath, 1, Qt::MatchExactly); QStandardItem *parent; if (parents.isEmpty()) { parent = new QStandardItem(QIcon(":/icons/16x16/folder.png"), directoryPath); parent->setToolTip(directoryPath); parent->setData(directoryPath, DirectoryPathRole); parent->setData(location.isEmpty() ? "" : directoryPath.toLower(), LowerCaseNameRole); m_model.appendRow(parent); m_model.sort(0); } else { parent = m_model.itemFromIndex(parents.first()); } // Create child item QStandardItem *child = new QStandardItem(QIcon(":/icons/16x16/file.png"), fileName); child->setToolTip(filePath); child->setData(qVariantFromValue((void *)document), DocumentPointerRole); child->setData(fileName, FileNameRole); child->setData(location.isEmpty() ? "" : fileName.toLower(), LowerCaseNameRole); m_children.insert(document, child); parent->appendRow(child); parent->sortChildren(0); connect(document, &Document::locationChanged, this, &FilesWidget::updateLocationOfSender); connect(document, &Document::modificationChanged, this, &FilesWidget::updateModificationMarkerOfSender); // Apply current filter if (!filterAcceptsChild(child->index())) { m_treeFiles->setRowHidden(child->row(), parent->index(), true); if (parent->rowCount() == 1) { m_treeFiles->setRowHidden(parent->row(), m_model.invisibleRootItem()->index(), true); } } // Show modification marker, if necessary updateModificationMarker(document); updateParentItemMarkers(parent); }
void TerminalWidget::tableView_sortIndicatorChanged(int logicalIndex, Qt::SortOrder order) { CentralModel* centralModel = dynamic_cast<CentralModel*>(ui->tableView->model()); if (!centralModel) return ; QModelIndex rootIndex = ui->tableView->rootIndex(); if (!rootIndex.isValid()) return ; QStandardItem* rootItem = centralModel->itemFromIndex(rootIndex); if (!rootItem) return ; rootItem->sortChildren(logicalIndex, order); }
void HistoryContentsWidget::addEntry(const HistoryEntry &entry) { if (entry.identifier < 0) { return; } QStandardItem *groupItem = NULL; for (int i = 0; i < m_model->rowCount(); ++i) { groupItem = m_model->item(i, 0); if (groupItem && (entry.time.date() >= groupItem->data(Qt::UserRole).toDate() || !groupItem->data(Qt::UserRole).toDate().isValid())) { break; } groupItem = NULL; } if (!groupItem) { return; } QList<QStandardItem*> entryItems; entryItems.append(new QStandardItem((entry.icon.isNull() ? Utils::getIcon(QLatin1String("text-html")) : entry.icon), entry.url.toString().replace(QLatin1String("%23"), QString(QLatin1Char('#'))))); entryItems.append(new QStandardItem(entry.title.isEmpty() ? tr("(Untitled)") : entry.title)); entryItems.append(new QStandardItem(entry.time.toString())); entryItems[0]->setData(entry.identifier, Qt::UserRole); groupItem->appendRow(entryItems); m_ui->historyView->setRowHidden(groupItem->row(), groupItem->index().parent(), false); if (sender()) { groupItem->sortChildren(2, Qt::DescendingOrder); } if (!m_ui->filterLineEdit->text().isEmpty()) { filterHistory(m_ui->filterLineEdit->text()); } }
void HistoryContentsWidget::populateEntries() { const QDate date = QDate::currentDate(); QList<QDate> dates; dates << date << date.addDays(-1) << date.addDays(-7) << date.addDays(-14) << date.addDays(-30) << date.addDays(-365); for (int i = 0; i < m_model->rowCount(); ++i) { m_ui->historyView->setRowHidden(i, m_model->invisibleRootItem()->index(), true); QStandardItem *groupItem = m_model->item(i, 0); if (groupItem) { groupItem->setData(dates.value(i, QDate()), Qt::UserRole); groupItem->removeRows(0, groupItem->rowCount()); } } const QList<HistoryEntry> entries = HistoryManager::getEntries(); for (int i = 0; i < entries.count(); ++i) { addEntry(entries.at(i)); } for (int i = 0; i < m_model->rowCount(); ++i) { QStandardItem *groupItem = m_model->item(i, 0); if (groupItem) { groupItem->sortChildren(2, Qt::DescendingOrder); } } m_isLoading = false; emit loadingChanged(false); }
void DayAvailabilityModel::addAvailability(const DayAvailability &availability) { Q_ASSERT(d->m_UserCalendar); Q_ASSERT(IN_RANGE(availability.weekDay(), Qt::Monday, Qt::Sunday)); d->m_UserCalendar->addAvailabilities(availability); // find the day item QStandardItem *dayItem = 0; for(int i = 0; i < invisibleRootItem()->rowCount(); ++i) { if (invisibleRootItem()->child(i)->data(WeekDayRole).toInt() == availability.weekDay()) { dayItem = invisibleRootItem()->child(i); break; } } // at this point there MUST be a valid dayItem. If not, something really bad happened. Q_ASSERT(dayItem); // delete the placeholder item, if exists if (dayItem->hasChildren() && dayItem->child(0)->data(AvailIdRole).toInt() == -1) dayItem->removeRow(0); // Insert the TimeRanges for(int i = 0; i < availability.timeRangeCount(); ++i) { TimeRange range = availability.timeRangeAt(i); QStandardItem *timeItem = new QStandardItem(tkTr(Trans::Constants::FROM_1_TO_2).arg(range.from.toString()).arg(range.to.toString())); timeItem->setData(availability.weekDay(), WeekDayRole); timeItem->setData(range.from, HourFromRole); timeItem->setData(range.to, HourToRole); timeItem->setToolTip(timeItem->text()); dayItem->appendRow(timeItem); } dayItem->sortChildren(0); }