void PlacesView::activateRow(int type, const QModelIndex& index) { if(!index.parent().isValid()) // ignore root items return; PlacesModelItem* item = static_cast<PlacesModelItem*>(model_->itemFromIndex(index)); if(item) { FmPath* path = item->path(); if(!path) { // check if mounting volumes is needed if(item->type() == PlacesModelItem::Volume) { PlacesModelVolumeItem* volumeItem = static_cast<PlacesModelVolumeItem*>(item); if(!volumeItem->isMounted()) { // Mount the volume GVolume* volume = volumeItem->volume(); MountOperation* op = new MountOperation(true, this); op->mount(volume); // connect(op, SIGNAL(finished(GError*)), SLOT(onMountOperationFinished(GError*))); // blocking here until the mount operation is finished? // FIXME: update status of the volume after mount is finished!! if(!op->wait()) return; path = item->path(); } } } if(path) { Q_EMIT chdirRequested(type, path); } } }
PlacesModelItem* PlacesModel::itemFromPath(QStandardItem* rootItem, FmPath* path) { int rowCount = rootItem->rowCount(); for(int i = 0; i < rowCount; ++i) { PlacesModelItem* item = static_cast<PlacesModelItem*>(rootItem->child(i, 0)); if(fm_path_equal(item->path(), path)) return item; } return NULL; }
void PlacesView::onOpenNewWindow() { PlacesModel::ItemAction* action = static_cast<PlacesModel::ItemAction*>(sender()); if(!action->index().isValid()) return; PlacesModelItem* item = static_cast<PlacesModelItem*>(model_->itemFromIndex(action->index())); if(item) Q_EMIT chdirRequested(2, item->path()); }
PlacesModelMountItem* PlacesModel::itemFromMount(GMount* mount) { int rowCount = devicesRoot->rowCount(); for(int i = 0; i < rowCount; ++i) { PlacesModelItem* item = static_cast<PlacesModelItem*>(devicesRoot->child(i, 0)); if(item->type() == PlacesModelItem::Mount) { PlacesModelMountItem* mountItem = static_cast<PlacesModelMountItem*>(item); if(mountItem->mount() == mount) return mountItem; } } return NULL; }
PlacesModelVolumeItem* PlacesModel::itemFromVolume(GVolume* volume) { int rowCount = devicesRoot->rowCount(); for(int i = 0; i < rowCount; ++i) { PlacesModelItem* item = static_cast<PlacesModelItem*>(devicesRoot->child(i, 0)); if(item->type() == PlacesModelItem::Volume) { PlacesModelVolumeItem* volumeItem = static_cast<PlacesModelVolumeItem*>(item); if(volumeItem->volume() == volume) return volumeItem; } } return NULL; }
void PlacesModel::updateIcons() { // the icon theme is changed and we need to update the icons PlacesModelItem* item; int row; int n = placesRoot->rowCount(); for(row = 0; row < n; ++row) { item = static_cast<PlacesModelItem*>(placesRoot->child(row)); item->updateIcon(); } n = devicesRoot->rowCount(); for(row = 0; row < n; ++row) { item = static_cast<PlacesModelItem*>(devicesRoot->child(row)); item->updateIcon(); } }
Qt::ItemFlags PlacesModel::flags(const QModelIndex& index) const { if(index.column() == 1) // make 2nd column of every row selectable. return Qt::ItemIsSelectable | Qt::ItemIsEnabled; if(!index.parent().isValid()) { // root items if(index.row() == 2) // bookmarks root return Qt::ItemIsEnabled | Qt::ItemIsDropEnabled; else return Qt::ItemIsEnabled; } PlacesModelItem* placesItem = static_cast<PlacesModelItem*>(itemFromIndex(index)); if(placesItem) { if(placesItem->type() == PlacesModelItem::Bookmark) return Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsDropEnabled | Qt::ItemIsDragEnabled; } return Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsDropEnabled; // return QStandardItemModel::flags(index); }
void PlacesView::setCurrentPath(FmPath* path) { if(currentPath_) fm_path_unref(currentPath_); if(path) { currentPath_ = fm_path_ref(path); // TODO: search for item with the path in model_ and select it. PlacesModelItem* item = model_->itemFromPath(currentPath_); if(item) { selectionModel()->select(item->index(), QItemSelectionModel::SelectCurrent|QItemSelectionModel::Rows); } else clearSelection(); } else { currentPath_ = NULL; clearSelection(); } }
void PlacesView::contextMenuEvent(QContextMenuEvent* event) { QModelIndex index = indexAt(event->pos()); if(index.isValid() && index.parent().isValid()) { if(index.column() != 0) // the real item is at column 0 index = index.sibling(index.row(), 0); // Do not take the ownership of the menu since // it will be deleted with deleteLater() upon hidden. // This is possibly related to #145 - https://github.com/lxde/pcmanfm-qt/issues/145 QMenu* menu = new QMenu(); QAction* action; PlacesModelItem* item = static_cast<PlacesModelItem*>(model_->itemFromIndex(index)); if(item->type() != PlacesModelItem::Mount && (item->type() != PlacesModelItem::Volume || static_cast<PlacesModelVolumeItem*>(item)->isMounted())) { action = new PlacesModel::ItemAction(item->index(), tr("Open in New Tab"), menu); connect(action, &QAction::triggered, this, &PlacesView::onOpenNewTab); menu->addAction(action); action = new PlacesModel::ItemAction(item->index(), tr("Open in New Window"), menu); connect(action, &QAction::triggered, this, &PlacesView::onOpenNewWindow); menu->addAction(action); } switch(item->type()) { case PlacesModelItem::Places: { FmPath* path = item->path(); if(path && fm_path_equal(fm_path_get_trash(), path)) { action = new PlacesModel::ItemAction(item->index(), tr("Empty Trash"), menu); connect(action, &QAction::triggered, this, &PlacesView::onEmptyTrash); menu->addAction(action); } break; } case PlacesModelItem::Bookmark: { // create context menu for bookmark item if(item->index().row() > 0) { action = new PlacesModel::ItemAction(item->index(), tr("Move Bookmark Up"), menu); connect(action, &QAction::triggered, this, &PlacesView::onMoveBookmarkUp); menu->addAction(action); } if(item->index().row() < model_->rowCount()) { action = new PlacesModel::ItemAction(item->index(), tr("Move Bookmark Down"), menu); connect(action, &QAction::triggered, this, &PlacesView::onMoveBookmarkDown); menu->addAction(action); } action = new PlacesModel::ItemAction(item->index(), tr("Rename Bookmark"), menu); connect(action, &QAction::triggered, this, &PlacesView::onRenameBookmark); menu->addAction(action); action = new PlacesModel::ItemAction(item->index(), tr("Remove Bookmark"), menu); connect(action, &QAction::triggered, this, &PlacesView::onDeleteBookmark); menu->addAction(action); break; } case PlacesModelItem::Volume: { PlacesModelVolumeItem* volumeItem = static_cast<PlacesModelVolumeItem*>(item); if(volumeItem->isMounted()) { action = new PlacesModel::ItemAction(item->index(), tr("Unmount"), menu); connect(action, &QAction::triggered, this, &PlacesView::onUnmountVolume); } else { action = new PlacesModel::ItemAction(item->index(), tr("Mount"), menu); connect(action, &QAction::triggered, this, &PlacesView::onMountVolume); } menu->addAction(action); if(volumeItem->canEject()) { action = new PlacesModel::ItemAction(item->index(), tr("Eject"), menu); connect(action, &QAction::triggered, this, &PlacesView::onEjectVolume); menu->addAction(action); } break; } case PlacesModelItem::Mount: { action = new PlacesModel::ItemAction(item->index(), tr("Unmount"), menu); connect(action, &QAction::triggered, this, &PlacesView::onUnmountMount); menu->addAction(action); break; } } if(menu->actions().size()) { menu->popup(mapToGlobal(event->pos())); connect(menu, &QMenu::aboutToHide, menu, &QMenu::deleteLater); } else { menu->deleteLater(); } } }