bool TreeSample::handleEventSelectionChanged(const CEGUI::EventArgs& args) { using namespace CEGUI; const ItemViewEventArgs& treeArgs = static_cast<const ItemViewEventArgs&>(args); Editbox *editBox = (Editbox *)d_rootWindow->getChild(EditBoxID); TreeWidget* tree = static_cast<TreeWidget*>(treeArgs.window); // Three different ways to get the item selected. // TreeCtrlEntry *selectedItem = theTree->getFirstSelectedItem(); // the first selection in the list (may be more) // TreeCtrlEntry *selectedItem = theTree->getLastSelectedItem(); // the last (time-wise) selected by the user StandardItem* selectedItem = tree->getModel()->getItemForIndex(treeArgs.d_index); // the actual item that caused this event if (selectedItem) { #if 0 // A convoluted way to get the item text. // Just here to test findFirstItemWithText. selectedItem = theTree->findFirstItemWithText(selectedItem->getText()); if (selectedItem) editBox->setText("Selected: " + selectedItem->getText()); else editBox->setText("findItemWithText failed!"); #else // The simple way to do it. editBox->setText("Selected: " + selectedItem->getText()); #endif } else editBox->setText("None Selected"); return true; }
void StandardTreeModel::writeTaskAndChildren(QXmlStreamWriter *writer, QStandardItem *root) { if (root != invisibleRootItem()) { StandardItem *item = static_cast<StandardItem*>(root); writer->writeStartElement(TaskTag); writer->writeAttribute(NameAttribute, item->text()); writer->writeAttribute(DoneAttribute, item->checkState() == Qt::Checked ? "1" : "0"); QListIterator< QPair<QDateTime, QDateTime> > i(item->dateTimes()); while (i.hasNext()) { const QPair<QDateTime, QDateTime> &dateTime = i.next(); writer->writeStartElement(WhenTag); writer->writeAttribute(StartAttribute, dateTime.first.toString(Qt::ISODate)); writer->writeAttribute(EndAttribute, dateTime.second.toString(Qt::ISODate)); writer->writeEndElement(); // WHEN } } for (int row = 0; row < root->rowCount(); ++row) writeTaskAndChildren(writer, root->child(row, 0)); if (root != invisibleRootItem()) writer->writeEndElement(); // TASK }
StandardItem *StandardTreeModel::createNewTask(QStandardItem *root, const QString &name, bool done) { StandardItem *nameItem = new StandardItem(name, done); root->appendRow(QList<QStandardItem*>() << nameItem << nameItem->todayItem() << nameItem->totalItem()); return nameItem; }
void StandardTreeModel::calculateTotalsFor(QStandardItem *root) { if (root != invisibleRootItem()) { StandardItem *item = static_cast<StandardItem*>(root); item->todayItem()->setText(item->todaysTime()); item->totalItem()->setText(item->totalTime()); } for (int row = 0; row < root->rowCount(); ++row) calculateTotalsFor(root->child(row, 0)); }
bool MenuNavigationSample::handleSelectButtonClicked(const CEGUI::EventArgs&) { using namespace CEGUI; StandardItem* item = d_classesList->getFirstSelectedItem(); if (item != 0) { d_logWidget2->setText("Selected " + item->getText() + "\n"); } return true; }
void StandardTreeModel::load(const QString &filename) { if (!filename.isEmpty()) m_filename = filename; if (m_filename.isEmpty()) throw AQP::Error(tr("no filename specified")); QFile file(m_filename); if (!file.open(QIODevice::ReadOnly)) throw AQP::Error(file.errorString()); clear(); QStack<QStandardItem*> stack; stack.push(invisibleRootItem()); QXmlStreamReader reader(&file); while (!reader.atEnd()) { reader.readNext(); if (reader.isStartElement()) { if (reader.name() == TaskTag) { const QString name = reader.attributes() .value(NameAttribute).toString(); bool done = reader.attributes().value(DoneAttribute) == "1"; StandardItem *nameItem = createNewTask(stack.top(), name, done); stack.push(nameItem); } else if (reader.name() == WhenTag) { const QDateTime start = QDateTime::fromString( reader.attributes().value(StartAttribute) .toString(), Qt::ISODate); const QDateTime end = QDateTime::fromString( reader.attributes().value(EndAttribute) .toString(), Qt::ISODate); StandardItem *nameItem = static_cast<StandardItem*>( stack.top()); nameItem->addDateTime(start, end); } } else if (reader.isEndElement()) { if (reader.name() == TaskTag) stack.pop(); } } if (reader.hasError()) throw AQP::Error(reader.errorString()); if (stack.count() != 1 || stack.top() != invisibleRootItem()) throw AQP::Error(tr("loading error: possibly corrupt file")); calculateTotalsFor(invisibleRootItem()); }
void FontsSample::changeFontSelectorFontSelection(const CEGUI::String& font) { d_fontSelector->clearSelections(); unsigned int itemCount = d_fontSelector->getItemCount(); for (unsigned int i = 0; i < itemCount; ++i) { StandardItem* item = d_fontSelector->getItemAtIndex(i); String itemFontName = item->getText(); if (itemFontName.compare(font) == 0) { d_fontSelector->setIndexSelectionState(i, true); return; } } }
int StandardItem::minutesForTask(bool onlyForToday) const { int minutes = 0; QListIterator<QPair<QDateTime, QDateTime> > i(m_dateTimes); while (i.hasNext()) { const QPair<QDateTime, QDateTime> &dateTime = i.next(); if (onlyForToday && dateTime.first.date() != QDate::currentDate()) continue; minutes += (dateTime.first.secsTo(dateTime.second) / 60); } for (int row = 0; row < rowCount(); ++row) { StandardItem *item = static_cast<StandardItem*>(child(row, 0)); Q_ASSERT(item); minutes += item->minutesForTask(onlyForToday); } return minutes; }
/************************************************************************* Sample specific initialisation goes here. *************************************************************************/ bool TreeSample::initialise(CEGUI::GUIContext* guiContext) { using namespace CEGUI; d_usedFiles = String(__FILE__); TreeWidget* theTree; StandardItem* newTreeCtrlEntryLvl1; // Level 1 TreeCtrlEntry (branch) StandardItem* newTreeCtrlEntryLvl2; // Level 2 TreeCtrlEntry (branch) StandardItem* newTreeCtrlEntryLvl3; // Level 3 TreeCtrlEntry (branch) StandardItem* newTreeCtrlEntryParent; Image * iconArray[9]; #if defined( __WIN32__ ) || defined( _WIN32 ) // Windows specific code. srand(static_cast<unsigned int>(time(NULL))); #endif // Get window manager which we will use for a few jobs here. WindowManager& winMgr = WindowManager::getSingleton(); // load font and setup default if not loaded via scheme Font& defaultFont = FontManager::getSingleton().createFromFile("DejaVuSans-12.font"); // Set default font for the gui context guiContext->setDefaultFont(&defaultFont); // Load the scheme to initialise the skin which we use in this sample SchemeManager::getSingleton().createFromFile(SCHEME_FILE_NAME); // set default cursor image guiContext->getCursor().setDefaultImage(IMAGES_FILE_NAME "/MouseArrow"); // load an image to use as a background if (!ImageManager::getSingleton().isDefined("SpaceBackgroundImage")) ImageManager::getSingleton().addBitmapImageFromFile("SpaceBackgroundImage", "SpaceBackground.jpg"); // Load some icon images for our test tree ImageManager::getSingleton().loadImageset("DriveIcons.imageset"); // here we will use a StaticImage as the root, then we can use it to place a background image Window* background = winMgr.createWindow(STATICIMAGE_NAME); // set area rectangle background->setArea(URect(cegui_reldim(0), cegui_reldim(0), cegui_reldim(1), cegui_reldim(1))); // disable frame and standard background background->setProperty("FrameEnabled", "false"); background->setProperty("BackgroundEnabled", "false"); // set the background image background->setProperty("Image", "SpaceBackgroundImage"); // install this as the root GUI sheet guiContext->setRootWindow(background); FontManager::getSingleton().createFromFile("DejaVuSans-12.font"); d_rootWindow = winMgr.loadLayoutFromFile(LAYOUT_FILE_NAME); background->addChild(d_rootWindow); theTree = static_cast<TreeWidget*>(d_rootWindow->getChild(TreeID)); theTree->setSelectionBrushImage(IMAGES_FILE_NAME BRUSH_NAME); theTree->subscribeEvent(TreeWidget::EventSelectionChanged, Event::Subscriber(&TreeSample::handleEventSelectionChanged, this)); theTree->subscribeEvent(TreeWidget::EventSubtreeExpanded, Event::Subscriber(&TreeSample::handleEventSubtreeExpanded, this)); theTree->subscribeEvent(TreeWidget::EventSubtreeCollapsed, Event::Subscriber(&TreeSample::handleEventSubtreeCollapsed, this)); // activate the background window background->activate(); iconArray[0] = &ImageManager::getSingleton().get("DriveIcons/Artic"); iconArray[1] = &ImageManager::getSingleton().get("DriveIcons/Black"); iconArray[2] = &ImageManager::getSingleton().get("DriveIcons/Sunset"); iconArray[3] = &ImageManager::getSingleton().get("DriveIcons/DriveStack"); iconArray[4] = &ImageManager::getSingleton().get("DriveIcons/GlobalDrive"); iconArray[5] = &ImageManager::getSingleton().get("DriveIcons/Blue"); iconArray[6] = &ImageManager::getSingleton().get("DriveIcons/Lime"); iconArray[7] = &ImageManager::getSingleton().get("DriveIcons/Silver"); iconArray[8] = &ImageManager::getSingleton().get("DriveIcons/GreenCandy"); // Create a top-most TreeCtrlEntry newTreeCtrlEntryLvl1 = new StandardItem("Tree Item Level 1a", "DriveIcons/Black"); theTree->addItem(newTreeCtrlEntryLvl1); // Create a second-level TreeCtrlEntry and attach it to the top-most TreeCtrlEntry newTreeCtrlEntryLvl2 = new StandardItem("Tree Item Level 2a (1a)"); newTreeCtrlEntryLvl1->addItem(newTreeCtrlEntryLvl2); // Create a third-level TreeCtrlEntry and attach it to the above TreeCtrlEntry newTreeCtrlEntryLvl3 = new StandardItem("Tree Item Level 3a (2a)", "DriveIcons/Blue"); newTreeCtrlEntryLvl2->addItem(newTreeCtrlEntryLvl3); // Create another third-level TreeCtrlEntry and attach it to the above TreeCtrlEntry newTreeCtrlEntryLvl3 = new StandardItem("Tree Item Level 3b (2a)", "DriveIcons/Lime"); newTreeCtrlEntryLvl2->addItem(newTreeCtrlEntryLvl3); // Create another second-level TreeCtrlEntry and attach it to the top-most TreeCtrlEntry newTreeCtrlEntryLvl2 = new StandardItem("Tree Item Level 2b (1a)", "DriveIcons/Sunset"); newTreeCtrlEntryLvl1->addItem(newTreeCtrlEntryLvl2); // Create another second-level TreeCtrlEntry and attach it to the top-most TreeCtrlEntry newTreeCtrlEntryLvl2 = new StandardItem("Tree Item Level 2c (1a)", "DriveIcons/Silver"); newTreeCtrlEntryLvl1->addItem(newTreeCtrlEntryLvl2); // Create another top-most TreeCtrlEntry newTreeCtrlEntryLvl1 = new StandardItem("Tree Item Level 1b", "DriveIcons/DriveStack"); theTree->addItem(newTreeCtrlEntryLvl1); // Create a second-level TreeCtrlEntry and attach it to the top-most TreeCtrlEntry newTreeCtrlEntryLvl2 = new StandardItem("Tree Item Level 2a (1b)"); newTreeCtrlEntryLvl1->addItem(newTreeCtrlEntryLvl2); // Create another second-level TreeCtrlEntry and attach it to the top-most TreeCtrlEntry newTreeCtrlEntryLvl2 = new StandardItem("Tree Item Level 2b (1b)"); newTreeCtrlEntryLvl1->addItem(newTreeCtrlEntryLvl2); newTreeCtrlEntryLvl1 = new StandardItem("Tree Item Level 1c"); theTree->addItem(newTreeCtrlEntryLvl1); // Now let's create a whole bunch of items automatically int levelIndex = 3; int idepthIndex; int childIndex; int childCount; unsigned int iconIndex; String itemText; while (levelIndex < 10) { idepthIndex = 0; itemText = "Tree Item Level " + PropertyHelper<int>::toString(levelIndex) + " Depth " + PropertyHelper<int>::toString(idepthIndex); newTreeCtrlEntryLvl1 = new StandardItem(itemText); // Set a random icon for the item. Sometimes blank (on purpose). iconIndex = randInt(0, (sizeof(iconArray) / sizeof(iconArray[0])) + 2); if (iconIndex < sizeof(iconArray) / sizeof(iconArray[0])) newTreeCtrlEntryLvl1->setIcon((*iconArray[iconIndex]).getName()); theTree->addItem(newTreeCtrlEntryLvl1); newTreeCtrlEntryParent = newTreeCtrlEntryLvl1; childIndex = 0; childCount = randInt(0, 3); while (childIndex < childCount) { itemText = "Tree Item Level " + PropertyHelper<int>::toString(levelIndex) + " Depth " + PropertyHelper<int>::toString(idepthIndex + 1) + " Child " + PropertyHelper<int>::toString(childIndex + 1); newTreeCtrlEntryLvl2 = new StandardItem(itemText); // Set a random icon for the item. Sometimes blank (on purpose). iconIndex = randInt(0, (sizeof(iconArray) / sizeof(iconArray[0]) + 2)); if (iconIndex < sizeof(iconArray) / sizeof(iconArray[0])) newTreeCtrlEntryLvl2->setIcon((*iconArray[iconIndex]).getName()); newTreeCtrlEntryParent->addItem(newTreeCtrlEntryLvl2); ++childIndex; } while (idepthIndex < 15) { itemText = "Tree Item Level " + PropertyHelper<int>::toString(levelIndex) + " Depth " + PropertyHelper<int>::toString(idepthIndex + 1); newTreeCtrlEntryLvl2 = new StandardItem(itemText); // Set a random icon for the item. Sometimes blank (on purpose). iconIndex = randInt(0, (sizeof(iconArray) / sizeof(iconArray[0]) + 2)); if (iconIndex < sizeof(iconArray) / sizeof(iconArray[0])) newTreeCtrlEntryLvl2->setIcon((*iconArray[iconIndex]).getName()); newTreeCtrlEntryParent->addItem(newTreeCtrlEntryLvl2); newTreeCtrlEntryParent = newTreeCtrlEntryLvl2; childIndex = 0; childCount = randInt(0, 3); while (childIndex < childCount) { itemText = "Tree Item Level " + PropertyHelper<int>::toString(levelIndex) + " Depth " + PropertyHelper<int>::toString(idepthIndex + 1) + " Child " + PropertyHelper<int>::toString(childIndex + 1); newTreeCtrlEntryLvl2 = new StandardItem(itemText); // Set a random icon for the item. Sometimes blank (on purpose). iconIndex = randInt(0, (sizeof(iconArray) / sizeof(iconArray[0]) + 2)); if (iconIndex < sizeof(iconArray) / sizeof(iconArray[0])) newTreeCtrlEntryLvl2->setIcon((*iconArray[iconIndex]).getName()); newTreeCtrlEntryParent->addItem(newTreeCtrlEntryLvl2); ++childIndex; } ++idepthIndex; } ++levelIndex; } return true; }