projectexplorer: fixed UI issues around renaming items
This commit is contained in:
@@ -170,6 +170,11 @@ QVariant ProjectViewModel::data(const QModelIndex &index, int role) const
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
case Qt::EditRole:
|
||||||
|
if (column_type == kName) {
|
||||||
|
return internal_item->GetLabel();
|
||||||
|
}
|
||||||
|
break;
|
||||||
case Qt::DecorationRole:
|
case Qt::DecorationRole:
|
||||||
// If this is the first column, return the Item's icon
|
// If this is the first column, return the Item's icon
|
||||||
if (column_type == kName) {
|
if (column_type == kName) {
|
||||||
|
|||||||
@@ -92,10 +92,6 @@ ProjectExplorer::ProjectExplorer(QWidget *parent) :
|
|||||||
// Set default icon size
|
// Set default icon size
|
||||||
SizeChangedSlot(kProjectIconSizeDefault);
|
SizeChangedSlot(kProjectIconSizeDefault);
|
||||||
|
|
||||||
// Set rename timer timeout
|
|
||||||
rename_timer_.setInterval(500);
|
|
||||||
connect(&rename_timer_, &QTimer::timeout, this, &ProjectExplorer::RenameTimerSlot);
|
|
||||||
|
|
||||||
connect(tree_view_, &ProjectExplorerTreeView::customContextMenuRequested, this, &ProjectExplorer::ShowContextMenu);
|
connect(tree_view_, &ProjectExplorerTreeView::customContextMenuRequested, this, &ProjectExplorer::ShowContextMenu);
|
||||||
connect(list_view_, &ProjectExplorerListView::customContextMenuRequested, this, &ProjectExplorer::ShowContextMenu);
|
connect(list_view_, &ProjectExplorerListView::customContextMenuRequested, this, &ProjectExplorer::ShowContextMenu);
|
||||||
connect(icon_view_, &ProjectExplorerIconView::customContextMenuRequested, this, &ProjectExplorer::ShowContextMenu);
|
connect(icon_view_, &ProjectExplorerIconView::customContextMenuRequested, this, &ProjectExplorer::ShowContextMenu);
|
||||||
@@ -137,8 +133,7 @@ void ProjectExplorer::Edit(Node *item)
|
|||||||
void ProjectExplorer::AddView(QAbstractItemView *view)
|
void ProjectExplorer::AddView(QAbstractItemView *view)
|
||||||
{
|
{
|
||||||
view->setModel(&sort_model_);
|
view->setModel(&sort_model_);
|
||||||
view->setEditTriggers(QAbstractItemView::NoEditTriggers);
|
view->setEditTriggers(QAbstractItemView::SelectedClicked);
|
||||||
connect(view, &QAbstractItemView::clicked, this, &ProjectExplorer::ItemClickedSlot);
|
|
||||||
connect(view, &QAbstractItemView::doubleClicked, this, &ProjectExplorer::ItemDoubleClickedSlot);
|
connect(view, &QAbstractItemView::doubleClicked, this, &ProjectExplorer::ItemDoubleClickedSlot);
|
||||||
connect(view->selectionModel(), &QItemSelectionModel::selectionChanged, this, &ProjectExplorer::ViewSelectionChanged);
|
connect(view->selectionModel(), &QItemSelectionModel::selectionChanged, this, &ProjectExplorer::ViewSelectionChanged);
|
||||||
connect(view, SIGNAL(DoubleClickedEmptyArea()), this, SLOT(ViewEmptyAreaDoubleClickedSlot()));
|
connect(view, SIGNAL(DoubleClickedEmptyArea()), this, SLOT(ViewEmptyAreaDoubleClickedSlot()));
|
||||||
@@ -147,9 +142,6 @@ void ProjectExplorer::AddView(QAbstractItemView *view)
|
|||||||
|
|
||||||
void ProjectExplorer::BrowseToFolder(const QModelIndex &index)
|
void ProjectExplorer::BrowseToFolder(const QModelIndex &index)
|
||||||
{
|
{
|
||||||
// Make sure any rename timers are stopped
|
|
||||||
rename_timer_.stop();
|
|
||||||
|
|
||||||
// Set appropriate views to this index
|
// Set appropriate views to this index
|
||||||
icon_view_->setRootIndex(index);
|
icon_view_->setRootIndex(index);
|
||||||
list_view_->setRootIndex(index);
|
list_view_->setRootIndex(index);
|
||||||
@@ -264,53 +256,19 @@ QAbstractItemView *ProjectExplorer::CurrentView() const
|
|||||||
return static_cast<QAbstractItemView*>(stacked_widget_->currentWidget());
|
return static_cast<QAbstractItemView*>(stacked_widget_->currentWidget());
|
||||||
}
|
}
|
||||||
|
|
||||||
void ProjectExplorer::ItemClickedSlot(const QModelIndex &index)
|
|
||||||
{
|
|
||||||
if (index.isValid()) {
|
|
||||||
if (CurrentView()->selectionModel()->selectedRows().size() == 1) {
|
|
||||||
if (clicked_index_ == index) {
|
|
||||||
// The item has been clicked more than once, start a timer for renaming
|
|
||||||
rename_timer_.start();
|
|
||||||
} else {
|
|
||||||
// Cache this index for the next click
|
|
||||||
clicked_index_ = index;
|
|
||||||
|
|
||||||
// If the rename timer had started, stop it now
|
|
||||||
rename_timer_.stop();
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
clicked_index_ = QModelIndex();
|
|
||||||
rename_timer_.stop();
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// Stop the rename timer
|
|
||||||
rename_timer_.stop();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void ProjectExplorer::ViewEmptyAreaDoubleClickedSlot()
|
void ProjectExplorer::ViewEmptyAreaDoubleClickedSlot()
|
||||||
{
|
{
|
||||||
// Ensure no attempts to rename are made
|
|
||||||
clicked_index_ = QModelIndex();
|
|
||||||
rename_timer_.stop();
|
|
||||||
|
|
||||||
emit DoubleClickedItem(nullptr);
|
emit DoubleClickedItem(nullptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ProjectExplorer::ItemDoubleClickedSlot(const QModelIndex &index)
|
void ProjectExplorer::ItemDoubleClickedSlot(const QModelIndex &index)
|
||||||
{
|
{
|
||||||
// Ensure no attempts to rename are made
|
|
||||||
clicked_index_ = QModelIndex();
|
|
||||||
rename_timer_.stop();
|
|
||||||
|
|
||||||
// Retrieve source item from index
|
// Retrieve source item from index
|
||||||
Node* i = static_cast<Node*>(sort_model_.mapToSource(index).internalPointer());
|
Node* i = static_cast<Node*>(sort_model_.mapToSource(index).internalPointer());
|
||||||
|
|
||||||
// If the item is a folder, browse to it
|
// If the item is a folder, browse to it
|
||||||
if (dynamic_cast<Folder*>(i) && (view_type() == ProjectToolbar::ListView || view_type() == ProjectToolbar::IconView)) {
|
if (dynamic_cast<Folder*>(i) && (view_type() == ProjectToolbar::ListView || view_type() == ProjectToolbar::IconView)) {
|
||||||
|
|
||||||
BrowseToFolder(index);
|
BrowseToFolder(index);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Emit a signal
|
// Emit a signal
|
||||||
@@ -335,16 +293,12 @@ void ProjectExplorer::DirUpSlot()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void ProjectExplorer::RenameTimerSlot()
|
void ProjectExplorer::RenameSelectedItem()
|
||||||
{
|
{
|
||||||
// Start editing this index
|
auto indexes = CurrentView()->selectionModel()->selectedRows();
|
||||||
CurrentView()->edit(clicked_index_);
|
if (!indexes.empty()) {
|
||||||
|
CurrentView()->edit(indexes.first());
|
||||||
// Reset clicked index state
|
}
|
||||||
clicked_index_ = QModelIndex();
|
|
||||||
|
|
||||||
// Stop rename timer
|
|
||||||
rename_timer_.stop();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ProjectExplorer::ShowContextMenu()
|
void ProjectExplorer::ShowContextMenu()
|
||||||
@@ -447,6 +401,11 @@ void ProjectExplorer::ShowContextMenu()
|
|||||||
if (context_menu_items_.size() == 1) {
|
if (context_menu_items_.size() == 1) {
|
||||||
menu.addSeparator();
|
menu.addSeparator();
|
||||||
|
|
||||||
|
auto rename_action = menu.addAction(tr("Rename"));
|
||||||
|
connect(rename_action, &QAction::triggered, this, &ProjectExplorer::RenameSelectedItem);
|
||||||
|
|
||||||
|
menu.addSeparator();
|
||||||
|
|
||||||
QAction* properties_action = menu.addAction(tr("P&roperties"));
|
QAction* properties_action = menu.addAction(tr("P&roperties"));
|
||||||
connect(properties_action, &QAction::triggered, this, &ProjectExplorer::ShowItemPropertiesDialog);
|
connect(properties_action, &QAction::triggered, this, &ProjectExplorer::ShowItemPropertiesDialog);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -160,15 +160,9 @@ private:
|
|||||||
QSortFilterProxyModel sort_model_;
|
QSortFilterProxyModel sort_model_;
|
||||||
ProjectViewModel model_;
|
ProjectViewModel model_;
|
||||||
|
|
||||||
QModelIndex clicked_index_;
|
|
||||||
|
|
||||||
QTimer rename_timer_;
|
|
||||||
|
|
||||||
QVector<Node*> context_menu_items_;
|
QVector<Node*> context_menu_items_;
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
void ItemClickedSlot(const QModelIndex& index);
|
|
||||||
|
|
||||||
void ViewEmptyAreaDoubleClickedSlot();
|
void ViewEmptyAreaDoubleClickedSlot();
|
||||||
|
|
||||||
void ItemDoubleClickedSlot(const QModelIndex& index);
|
void ItemDoubleClickedSlot(const QModelIndex& index);
|
||||||
@@ -177,7 +171,7 @@ private slots:
|
|||||||
|
|
||||||
void DirUpSlot();
|
void DirUpSlot();
|
||||||
|
|
||||||
void RenameTimerSlot();
|
void RenameSelectedItem();
|
||||||
|
|
||||||
void ShowContextMenu();
|
void ShowContextMenu();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user