int TreeModel::rowCount(const QModelIndex &parent) const { if (parent.isValid() && parent.column() != 0) return 0; TaskItem *parentItem = itemForIndex(parent); return parentItem ? parentItem->childCount() : 0; }
void Task::setTask(AbstractGroupableItem *abstractItem) { if (m_abstractItem) { disconnect(m_abstractItem, SIGNAL(destroyed()), this, SLOT(validate())); } m_abstractItem = abstractItem; m_command = QString(); m_launcherUrl = KUrl(); if (m_abstractItem) { if (m_validateTimer > 0) { killTimer(m_validateTimer); m_validateTimer = 0; } connect(m_abstractItem, SIGNAL(destroyed()), this, SLOT(validate())); } if (m_abstractItem->itemType() == TaskManager::GroupItemType) { m_group = qobject_cast<TaskGroup*>(abstractItem); m_taskType = GroupType; if (m_applet->groupManager()->groupingStrategy() != TaskManager::GroupManager::ManualGrouping && m_group->members().count()) { TaskItem *task = qobject_cast<TaskItem*>(m_group->members().first()); if (task && task->task()) { if (m_group->name().isEmpty()) { m_group->setName(task->task()->visibleName()); } if (m_applet->groupManager()->groupingStrategy() == TaskManager::GroupManager::ProgramGrouping) { m_command = command(task->task()->pid()); } } } QList<WId> windowList = windows(); for (int i = 0; i < windowList.count(); ++i) { emit windowAdded(windowList.at(i)); } connect(m_group, SIGNAL(changed(::TaskManager::TaskChanges)), this, SLOT(taskChanged(::TaskManager::TaskChanges))); connect(m_group, SIGNAL(groupEditRequest()), this, SLOT(showPropertiesDialog())); connect(m_group, SIGNAL(itemAdded(AbstractGroupableItem*)), this, SLOT(addItem(AbstractGroupableItem*))); connect(m_group, SIGNAL(itemRemoved(AbstractGroupableItem*)), this, SLOT(removeItem(AbstractGroupableItem*))); }
/** * \brief Find the index of the entry with the matching task id */ int TaskMainWindow::indexForTask(int taskid) { for (int i = 0; i < ui->tasklistWidget->count(); i++) { QListWidgetItem *lwi = ui->tasklistWidget->item(i); TaskItem *ti = (TaskItem *)ui->tasklistWidget->itemWidget(lwi); if (ti->id() == taskid) { return i; } } return -1; }
QModelIndex TreeModel::moveUp(const QModelIndex &index) { if (!index.isValid() || index.row() <= 0) return index; TaskItem *item = itemForIndex(index); Q_ASSERT(item); TaskItem *parent = item->parent(); Q_ASSERT(parent); return moveItem(parent, index.row(), index.row() - 1); }
QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent) const { if (!rootItem || row < 0 || column < 0 || column >= ColumnCount || (parent.isValid() && parent.column() != 0)) return QModelIndex(); TaskItem *parentItem = itemForIndex(parent); Q_ASSERT(parentItem); if (TaskItem *item = parentItem->childAt(row)) return createIndex(row, column, item); return QModelIndex(); }
QModelIndex TreeModel::moveDown(const QModelIndex &index) { if (!index.isValid()) return index; TaskItem *item = itemForIndex(index); Q_ASSERT(item); TaskItem *parent = item->parent(); int newRow = index.row() + 1; if (!parent || parent->childCount() <= newRow) return index; return moveItem(parent, index.row(), newRow); }
/** * \brief Remove a task from the tasklist * * This method deletes task list entries. It is called from the timer tick * method and the taskRealUpdate slot. */ void TaskMainWindow::remove(int taskid) { // find the item in the list that matches the id for (int i = 0; i < ui->tasklistWidget->count(); i++) { QListWidgetItem *lwi = ui->tasklistWidget->item(i); TaskItem *ti = (TaskItem *)ui->tasklistWidget->itemWidget(lwi); if (ti->id() == taskid) { QListWidgetItem *lwi = ui->tasklistWidget->takeItem(i); delete lwi; return; } } }
bool TreeModel::removeRows(int row, int count, const QModelIndex &parent) { if (!rootItem) return false; TaskItem *item = parent.isValid() ? itemForIndex(parent) : rootItem; beginRemoveRows(parent, row, row + count - 1); for (int i = 0; i < count; ++i) delete item->takeChild(row); endRemoveRows(); return true; }
/** * \brief Retrieve a list of selected task ids * * This method scans the task list and constructs a list of tasks that * are selected. */ std::list<long> TaskMainWindow::selectedTaskids() { debug(LOG_DEBUG, DEBUG_LOG, 0, "retrieve a list of selected items"); std::list<long> taskids; for (int i = 0; i < ui->tasklistWidget->count(); i++) { QListWidgetItem *item = ui->tasklistWidget->item(i); if (item->isSelected()) { TaskItem *ti = (TaskItem *)ui->tasklistWidget->itemWidget(item); taskids.push_back(ti->id()); } } debug(LOG_DEBUG, DEBUG_LOG, 0, "%d selected task ids", taskids.size()); return taskids; }
int TaskItemModel::rowCount(const QModelIndex &parent) const { if (parent.column() > 0) { return 0; } TaskItem *parentTaskItem; if (!parent.isValid()) parentTaskItem = m_taskManager->rootItem(); else parentTaskItem = static_cast<TaskItem *>(parent.internalPointer()); return parentTaskItem->count(); }
void Task::addItem(AbstractGroupableItem *abstractItem) { if (abstractItem->itemType() != TaskManager::GroupItemType) { TaskItem *task = qobject_cast<TaskItem*>(abstractItem); if (task->task()) { emit windowAdded(task->task()->window()); } } emit changed(WindowsChanged); }
void TreeModel::announceItemChanged(TaskItem *item) { if (item == rootItem) return; TaskItem *parent = item->parent(); Q_ASSERT(parent); int row = parent->rowOfChild(item); QModelIndex startIndex = createIndex(row, static_cast<int>(Name), item); QModelIndex endIndex = createIndex(row, static_cast<int>(Total), item); emit dataChanged(startIndex, endIndex); // Update the parent & parent's parent etc too announceItemChanged(parent); }
QModelIndex TaskItemModel::parent(const QModelIndex &index) const { if (!index.isValid()) { return QModelIndex(); } TaskItem *childTaskItem = static_cast<TaskItem *>(index.internalPointer()); TaskItem *parentTaskItem = childTaskItem->parent(); if (parentTaskItem == m_taskManager->rootItem()) { return QModelIndex(); } return createIndex(parentTaskItem->calcIndex(), 0, parentTaskItem); }
bool TreeModel::insertRows(int row, int count, const QModelIndex &parent) { if (!rootItem) rootItem = new TaskItem; TaskItem *parentItem = parent.isValid() ? itemForIndex(parent) : rootItem; beginInsertRows(parent, row, row + count - 1); for (int i = 0; i < count; ++i) { TaskItem *item = new TaskItem(tr("New Task"), false); parentItem->insertChild(row, item); } endInsertRows(); return true; }
QModelIndex TaskItemModel::index(int row, int column, const QModelIndex &parent) const { if (!hasIndex(row, column, parent)) { return QModelIndex(); } TaskItem *parentTaskItem; if (!parent.isValid()) parentTaskItem = m_taskManager->rootItem(); else parentTaskItem = static_cast<TaskItem *>(parent.internalPointer()); return createIndex(row, column, parentTaskItem->value(row)); }
QModelIndex TreeModel::paste(const QModelIndex &index) { if (!index.isValid() || !cutItem) return index; TaskItem *sibling = itemForIndex(index); Q_ASSERT(sibling); TaskItem *parent = sibling->parent(); Q_ASSERT(parent); int row = parent->rowOfChild(sibling) + 1; beginInsertRows(index.parent(), row, row); parent->insertChild(row, cutItem); TaskItem *child = cutItem; cutItem = 0; endInsertRows(); return createIndex(row, 0, child); }
QModelIndex TreeModel::cut(const QModelIndex &index) { if (!index.isValid()) return index; delete cutItem; cutItem = itemForIndex(index); Q_ASSERT(cutItem); TaskItem *parent = cutItem->parent(); Q_ASSERT(parent); int row = parent->rowOfChild(cutItem); Q_ASSERT(row == index.row()); beginRemoveRows(index.parent(), row, row); TaskItem *child = parent->takeChild(row); endRemoveRows(); Q_ASSERT(child == cutItem); child = 0; // Silence compiler unused variable warning if (row > 0) { --row; return createIndex(row, 0, parent->childAt(row)); } if (parent != rootItem) { TaskItem *grandParent = parent->parent(); Q_ASSERT(grandParent); return createIndex(grandParent->rowOfChild(parent), 0, parent); } return QModelIndex(); }
void Task::removeItem(AbstractGroupableItem *abstractItem) { if (m_group && m_group->members().count() == 1) { m_taskType = TaskType; m_abstractItem = m_group->members().first(); m_group = NULL; m_task = qobject_cast<TaskItem*>(m_abstractItem); } TaskItem *task = qobject_cast<TaskItem*>(abstractItem); if (task && task->task()) { emit windowRemoved(task->task()->window()); } emit changed(EveythingChanged); }
void Task::publishIconGeometry(const QRect &geometry) { if (m_task && m_task->task()) { m_task->task()->publishIconGeometry(geometry); } else if (m_group) { QList<AbstractGroupableItem*> items = m_group->members(); for (int i = 0; i < items.count(); ++i) { if (items.at(i)->itemType() == TaskManager::TaskItemType) { TaskItem *task = qobject_cast<TaskItem*>(items.at(i)); task->task()->publishIconGeometry(geometry); } } } }
void GroupManagerPrivate::actuallyRemoveStartup() { if (startupRemoveList.isEmpty()) { return; } ::TaskManager::Startup *startup = startupRemoveList.takeFirst(); if (!startupList.contains(startup)) { return; } TaskItem *item = startupList.take(startup); if (item->parentGroup()) { item->parentGroup()->remove(item); } item->setTaskPointer(0); foreach (LauncherItem * launcher, launchers) { launcher->removeItemIfAssociated(item); }
void Task::kill() { KSysGuard::Processes processes; processes.updateAllProcesses(); if (m_taskType == TaskType && m_task) { processes.killProcess(m_task->task()->pid()); } else if (m_taskType == GroupType && m_group) { QList<AbstractGroupableItem*> members = m_group->members(); for (int i = 0; i < members.count(); ++i) { TaskItem *task = qobject_cast<TaskItem*>(members.at(i)); if (task) { processes.killProcess(task->task()->pid()); } } } }
QModelIndex TreeModel::demote(const QModelIndex &index) { if (!index.isValid()) return index; TaskItem *item = itemForIndex(index); Q_ASSERT(item); TaskItem *parent = item->parent(); Q_ASSERT(parent); int row = parent->rowOfChild(item); if (row == 0) return index; // No preceding sibling to move this under TaskItem *child = parent->takeChild(row); Q_ASSERT(child == item); TaskItem *sibling = parent->childAt(row - 1); Q_ASSERT(sibling); sibling->addChild(child); QModelIndex newIndex = createIndex(sibling->childCount() - 1, 0, child); emit dataChanged(newIndex, newIndex); return newIndex; }
QModelIndex TreeModel::promote(const QModelIndex &index) { if (!index.isValid()) return index; TaskItem *item = itemForIndex(index); Q_ASSERT(item); TaskItem *parent = item->parent(); Q_ASSERT(parent); if (parent == rootItem) return index; // Already a top-level item int row = parent->rowOfChild(item); TaskItem *child = parent->takeChild(row); Q_ASSERT(child == item); TaskItem *grandParent = parent->parent(); Q_ASSERT(grandParent); row = grandParent->rowOfChild(parent) + 1; grandParent->insertChild(row, child); QModelIndex newIndex = createIndex(row, 0, child); emit dataChanged(newIndex, newIndex); return newIndex; }
/** * \brief Really do the update * * When the timer's action slot finds out that some task ids have changed, * this slot is called to actually perform the udpate */ void TaskMainWindow::taskRealUpdate(int taskid) { debug(LOG_DEBUG, DEBUG_LOG, 0, "real task update %d", taskid); // retrieve the task information from the Astro::TaskInfo_var info; try { info = taskqueue->info(taskid); } catch (Astro::NotFound x) { debug(LOG_DEBUG, DEBUG_LOG, 0, "%d not found, removing it", taskid); remove(taskid); return; } catch (const std::exception& x) { debug(LOG_DEBUG, DEBUG_LOG, 0, "cannot get task info: %s", x.what()); return; } catch (const CORBA::SystemException& x) { debug(LOG_DEBUG, DEBUG_LOG, 0, "corba system exception"); return; } catch (const CORBA::UserException& x) { debug(LOG_DEBUG, DEBUG_LOG, 0, "corba user exception"); return; } catch (const CORBA::Exception& x) { debug(LOG_DEBUG, DEBUG_LOG, 0, "corba exception"); return; } catch (...) { debug(LOG_ERR, DEBUG_LOG, 0, "unknown error"); throw; return; } time_t now = time(NULL); info->lastchange = now - info->lastchange; debug(LOG_DEBUG, DEBUG_LOG, 0, "got updated info for task %d", taskid); // depending on whether the task already exists in the list, // we will add or udpate the stuff int index = indexForTask(taskid); if (index < 0) { debug(LOG_DEBUG, DEBUG_LOG, 0, "must insert entry"); // to insert an entry, we also need the parameters Astro::TaskParameters_var params = taskqueue->parameters(taskid); debug(LOG_DEBUG, DEBUG_LOG, 0, "task parameters for %d retrieved", taskid); // create a list item QListWidgetItem *lwi = new QListWidgetItem(); lwi->setSizeHint(QSize(300,90)); TaskItem *ti = new TaskItem(info, params); ui->tasklistWidget->addItem(lwi); ui->tasklistWidget->setItemWidget(lwi, ti); // make sure the list is repainted repaint(); // insert case done debug(LOG_DEBUG, DEBUG_LOG, 0, "entry for task %d inserted", taskid); return; } // retrieve the task TaskItem from the list, debug(LOG_DEBUG, DEBUG_LOG, 0, "we have to update task %d", taskid); QListWidgetItem *lwi = ui->tasklistWidget->item(index); TaskItem *ti = (TaskItem *)ui->tasklistWidget->itemWidget(lwi); debug(LOG_DEBUG, DEBUG_LOG, 0, "update entry %d", index); ti->updateInfo(info); // we have to repaint the item, because otherwise it will only repaint // when the it becomes visible after a list move, or when the window // receives a repaint event. ti->repaint(); }