diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 2ad203283..05cfb51ce 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -253,6 +253,7 @@ set(SOURCES widgets/prettyimageview.cpp widgets/progressitemdelegate.cpp widgets/ratingwidget.cpp + widgets/renametablineedit.cpp widgets/sliderwidget.cpp widgets/spinbox.cpp widgets/stickyslider.cpp @@ -440,6 +441,7 @@ set(HEADERS widgets/prettyimageview.h widgets/progressitemdelegate.h widgets/ratingwidget.h + widgets/renametablineedit.h widgets/sliderwidget.h widgets/spinbox.h widgets/stickyslider.h diff --git a/src/playlist/playlisttabbar.cpp b/src/playlist/playlisttabbar.cpp index 52f89c08b..189b86a73 100644 --- a/src/playlist/playlisttabbar.cpp +++ b/src/playlist/playlisttabbar.cpp @@ -18,9 +18,11 @@ #include "playlist.h" #include "playlistmanager.h" #include "playlisttabbar.h" +#include "playlistview.h" #include "songmimedata.h" #include "radio/radiomimedata.h" #include "ui/iconloader.h" +#include "widgets/renametablineedit.h" #include #include @@ -32,15 +34,22 @@ PlaylistTabBar::PlaylistTabBar(QWidget *parent) manager_(NULL), menu_(new QMenu(this)), menu_index_(-1), - suppress_current_changed_(false) + suppress_current_changed_(false), + rename_editor_(new RenameTabLineEdit(this)) { setAcceptDrops(true); + setElideMode(Qt::ElideRight); + setUsesScrollButtons(false); remove_ = menu_->addAction(IconLoader::Load("list-remove"), tr("Remove playlist"), this, SLOT(Remove())); rename_ = menu_->addAction(IconLoader::Load("edit-rename"), tr("Rename playlist..."), this, SLOT(Rename())); save_ = menu_->addAction(IconLoader::Load("document-save"), tr("Save playlist..."), this, SLOT(Save())); menu_->addSeparator(); + rename_editor_->setVisible(false); + connect(rename_editor_, SIGNAL(editingFinished()), SLOT(RenameInline())); + connect(rename_editor_, SIGNAL(EditingCanceled()), SLOT(HideEditor())); + connect(this, SIGNAL(currentChanged(int)), this, SLOT(CurrentIndexChanged(int))); connect(this, SIGNAL(tabMoved(int,int)), this, SLOT(TabMoved())); } @@ -58,6 +67,12 @@ void PlaylistTabBar::SetManager(PlaylistManager *manager) { } void PlaylistTabBar::contextMenuEvent(QContextMenuEvent* e) { + //we need to finish the renaming action before showing context menu + if (rename_editor_->isVisible()) { + //discard any change + HideEditor(); + } + menu_index_ = tabAt(e->pos()); rename_->setEnabled(menu_index_ != -1); remove_->setEnabled(menu_index_ != -1 && count() > 1); @@ -82,6 +97,16 @@ void PlaylistTabBar::mouseDoubleClickEvent(QMouseEvent *e) { if (index == -1) { new_->activate(QAction::Trigger); } + else { + //update current tab + menu_index_ = index; + + //set position + rename_editor_->setGeometry(tabRect(index)); + rename_editor_->setText(tabText(index)); + rename_editor_->setVisible(true); + rename_editor_->setFocus(); + } QTabBar::mouseDoubleClickEvent(e); } @@ -101,6 +126,19 @@ void PlaylistTabBar::Rename() { emit Rename(tabData(menu_index_).toInt(), name); } +void PlaylistTabBar::RenameInline() { + emit Rename(tabData(menu_index_).toInt(), rename_editor_->text()); + HideEditor(); +} + +void PlaylistTabBar::HideEditor() { + //editingFinished() will be called twice due to Qt bug #40, so we reuse the same instance, don't delete it + rename_editor_->setVisible(false); + + //hack to give back focus to playlist view + manager_->SetCurrentPlaylist(manager_->current()->id()); +} + void PlaylistTabBar::Remove() { if (menu_index_ == -1) return; diff --git a/src/playlist/playlisttabbar.h b/src/playlist/playlisttabbar.h index 741c17332..dffc1809c 100644 --- a/src/playlist/playlisttabbar.h +++ b/src/playlist/playlisttabbar.h @@ -22,6 +22,7 @@ #include class PlaylistManager; +class RenameTabLineEdit; class QMenu; @@ -69,6 +70,8 @@ protected: private slots: void CurrentIndexChanged(int index); void Rename(); + void RenameInline(); + void HideEditor(); void Remove(); void TabMoved(); void Save(); @@ -87,6 +90,9 @@ private: int drag_hover_tab_; bool suppress_current_changed_; + + //editor for inline renaming + RenameTabLineEdit* rename_editor_; }; #endif // PLAYLISTTABBAR_H diff --git a/src/widgets/renametablineedit.cpp b/src/widgets/renametablineedit.cpp new file mode 100644 index 000000000..9598e30e2 --- /dev/null +++ b/src/widgets/renametablineedit.cpp @@ -0,0 +1,41 @@ +/* This file is part of Clementine. + Copyright 2011, Andrea Decorte + + Clementine is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + Clementine is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with Clementine. If not, see . +*/ + +#include "renametablineedit.h" + +#include + +RenameTabLineEdit::RenameTabLineEdit(QWidget *parent) : + QLineEdit(parent) +{ +} + +void RenameTabLineEdit::keyPressEvent (QKeyEvent *e) { + if (e->key() == Qt::Key_Escape) { + e->accept(); + emit EditingCanceled(); + } + else { + QLineEdit::keyPressEvent(e); + } +} + +void RenameTabLineEdit::focusOutEvent(QFocusEvent *e) { + //if the user hasn't explicitly accepted, discard the value + emit EditingCanceled(); + //we don't call the default event since it will trigger editingFished() +} diff --git a/src/widgets/renametablineedit.h b/src/widgets/renametablineedit.h new file mode 100644 index 000000000..370ae8cad --- /dev/null +++ b/src/widgets/renametablineedit.h @@ -0,0 +1,39 @@ +/* This file is part of Clementine. + Copyright 2011, Andrea Decorte + + Clementine is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + Clementine is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with Clementine. If not, see . +*/ + +#ifndef RENAMETABLINEEDIT_H +#define RENAMETABLINEEDIT_H + +#include + +class RenameTabLineEdit : public QLineEdit { + Q_OBJECT + +public: + RenameTabLineEdit(QWidget* parent = 0); + +signals: + void EditingCanceled(); + +public slots: + +protected: + void focusOutEvent(QFocusEvent* e); + void keyPressEvent(QKeyEvent* e); +}; + +#endif // RENAMETABLINEEDIT_H