Commit 5f6fba86 authored by Waqar Ahmed's avatar Waqar Ahmed Committed by Christoph Cullmann
Browse files

Show a close button on hover in Documents plugin

parent 7cf04caf
......@@ -30,12 +30,49 @@
#include <QClipboard>
#include <QContextMenuEvent>
#include <QDir>
#include <QHeaderView>
#include <QInputDialog>
#include <QLineEdit>
#include <QMenu>
#include <QMimeDatabase>
#include <QStyledItemDelegate>
// END Includes
class StyleDelegate : public QStyledItemDelegate
{
public:
StyleDelegate(QObject *parent = nullptr)
: QStyledItemDelegate(parent)
{
}
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QStyledItemDelegate::paint(painter, option, index);
if (!m_closeBtn) {
return;
}
auto doc = index.data(KateFileTreeModel::DocumentRole).value<KTextEditor::Document *>();
if (doc && index.column() == 1 && option.state & QStyle::State_MouseOver) {
const QIcon icon = QIcon::fromTheme(QStringLiteral("document-close"));
QRect iconRect(option.rect.right() - option.rect.height(), option.rect.y(), option.rect.height(), option.rect.height());
icon.paint(painter, iconRect, Qt::AlignRight | Qt::AlignVCenter);
}
}
void setShowCloseButton(bool s)
{
m_closeBtn = s;
}
private:
bool m_closeBtn;
};
// BEGIN KateFileTree
KateFileTree::KateFileTree(QWidget *parent)
......@@ -47,6 +84,9 @@ KateFileTree::KateFileTree(QWidget *parent)
setFocusPolicy(Qt::NoFocus);
setDragEnabled(true);
setDragDropMode(QAbstractItemView::DragOnly);
setSelectionBehavior(QAbstractItemView::SelectRows);
setItemDelegate(new StyleDelegate(this));
// handle activated (e.g. for pressing enter) + clicked (to avoid to need to do double-click e.g. on Windows)
connect(this, &KateFileTree::activated, this, &KateFileTree::mouseClicked);
......@@ -144,6 +184,18 @@ void KateFileTree::setModel(QAbstractItemModel *model)
{
Q_ASSERT(qobject_cast<KateFileTreeProxyModel *>(model)); // we don't really work with anything else
QTreeView::setModel(model);
header()->hide();
header()->setStretchLastSection(false);
header()->setSectionResizeMode(0, QHeaderView::Stretch);
header()->setSectionResizeMode(1, QHeaderView::Fixed);
header()->resizeSection(1, 16);
}
void KateFileTree::setShowCloseButton(bool show)
{
m_hasCloseButton = show;
static_cast<StyleDelegate *>(itemDelegate())->setShowCloseButton(show);
}
QAction *KateFileTree::setupOption(QActionGroup *group, const QIcon &icon, const QString &label, const QString &whatsThis, const char *slot, bool checked)
......@@ -198,6 +250,10 @@ void KateFileTree::slotCurrentChanged(const QModelIndex &current, const QModelIn
void KateFileTree::mouseClicked(const QModelIndex &index)
{
if (auto doc = model()->data(index, KateFileTreeModel::DocumentRole).value<KTextEditor::Document *>()) {
if (m_hasCloseButton && index.column() == 1) {
KTextEditor::Editor::instance()->application()->closeDocuments({doc});
return;
}
Q_EMIT activateDocument(doc);
}
}
......@@ -206,7 +262,7 @@ void KateFileTree::contextMenuEvent(QContextMenuEvent *event)
{
m_indexContextMenu = selectionModel()->currentIndex();
selectionModel()->setCurrentIndex(m_indexContextMenu, QItemSelectionModel::ClearAndSelect);
selectionModel()->setCurrentIndex(m_indexContextMenu, QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
KateFileTreeProxyModel *ftpm = static_cast<KateFileTreeProxyModel *>(model());
KateFileTreeModel *ftm = static_cast<KateFileTreeModel *>(ftpm->sourceModel());
......@@ -264,7 +320,7 @@ void KateFileTree::contextMenuEvent(QContextMenuEvent *event)
menu.exec(viewport()->mapToGlobal(event->pos()));
if (m_previouslySelected.isValid()) {
selectionModel()->setCurrentIndex(m_previouslySelected, QItemSelectionModel::ClearAndSelect);
selectionModel()->setCurrentIndex(m_previouslySelected, QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
}
event->accept();
......
......@@ -27,6 +27,7 @@ public:
~KateFileTree() override;
void setModel(QAbstractItemModel *model) override;
void setShowCloseButton(bool show);
public Q_SLOTS:
void slotDocumentClose();
......@@ -98,6 +99,8 @@ private:
QPersistentModelIndex m_previouslySelected;
QPersistentModelIndex m_indexContextMenu;
bool m_hasCloseButton;
};
#endif // KATE_FILETREE_H
......@@ -89,6 +89,10 @@ KateFileTreeConfigPage::KateFileTreeConfigPage(QWidget *parent, KateFileTreePlug
cbShowFullPath = new QCheckBox(i18n("&Show Full Path"), this);
lo4->addWidget(cbShowFullPath);
cbShowClose = new QCheckBox(i18n("Show Close Button"), this);
layout->addWidget(cbShowClose);
layout->addWidget(new QLabel(i18n("When enabled, this will show a close button for opened documents on hover.")));
layout->insertStretch(-1, 10);
gbEnableShading->setWhatsThis(
......@@ -116,6 +120,7 @@ KateFileTreeConfigPage::KateFileTreeConfigPage(QWidget *parent, KateFileTreePlug
connect(cmbSort, QOverload<int>::of(&QComboBox::activated), this, &KateFileTreeConfigPage::slotMyChanged);
connect(cmbMode, QOverload<int>::of(&QComboBox::activated), this, &KateFileTreeConfigPage::slotMyChanged);
connect(cbShowFullPath, &QCheckBox::stateChanged, this, &KateFileTreeConfigPage::slotMyChanged);
connect(cbShowClose, &QCheckBox::stateChanged, this, &KateFileTreeConfigPage::slotMyChanged);
}
QString KateFileTreeConfigPage::name() const
......@@ -147,7 +152,8 @@ void KateFileTreeConfigPage::apply()
kcbEditShade->color(),
cmbMode->itemData(cmbMode->currentIndex()).toBool(),
cmbSort->itemData(cmbSort->currentIndex()).toInt(),
cbShowFullPath->checkState() == Qt::Checked);
cbShowFullPath->checkState() == Qt::Checked,
cbShowClose->isChecked());
}
void KateFileTreeConfigPage::reset()
......@@ -160,6 +166,7 @@ void KateFileTreeConfigPage::reset()
cmbSort->setCurrentIndex(cmbSort->findData(settings.sortRole()));
cmbMode->setCurrentIndex(settings.listMode());
cbShowFullPath->setCheckState(settings.showFullPathOnRoots() ? Qt::Checked : Qt::Unchecked);
cbShowClose->setChecked(settings.showCloseButton());
m_changed = false;
}
......
......@@ -47,6 +47,7 @@ private:
class QLabel *lEditShade, *lViewShade, *lSort, *lMode;
QComboBox *cmbSort, *cmbMode;
class QCheckBox *cbShowFullPath;
class QCheckBox *cbShowClose;
KateFileTreePlugin *m_plug;
bool m_changed = false;
......
......@@ -536,15 +536,19 @@ QVariant KateFileTreeModel::data(const QModelIndex &index, int role) const
case Qt::DisplayRole:
// in list mode we want to use kate's fancy names.
if (m_listMode) {
return item->documentName();
} else {
return item->display();
if (index.column() == 0) {
if (m_listMode) {
return item->documentName();
} else {
return item->display();
}
}
break;
case Qt::DecorationRole:
return item->icon();
if (index.column() == 0) {
return item->icon();
}
break;
case Qt::ToolTipRole: {
QString tooltip = item->path();
if (item->flag(ProxyItem::DeletedExternally) || item->flag(ProxyItem::ModifiedExternally)) {
......@@ -623,7 +627,7 @@ int KateFileTreeModel::rowCount(const QModelIndex &parent) const
int KateFileTreeModel::columnCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return 1;
return 2;
}
QModelIndex KateFileTreeModel::parent(const QModelIndex &index) const
......@@ -651,7 +655,7 @@ QModelIndex KateFileTreeModel::parent(const QModelIndex &index) const
QModelIndex KateFileTreeModel::index(int row, int column, const QModelIndex &parent) const
{
const ProxyItem *p = nullptr;
if (column != 0) {
if (column != 0 && column != 1) {
return QModelIndex();
}
......@@ -669,7 +673,7 @@ QModelIndex KateFileTreeModel::index(int row, int column, const QModelIndex &par
return QModelIndex();
}
return createIndex(row, 0, p->child(row));
return createIndex(row, column, p->child(row));
}
bool KateFileTreeModel::hasChildren(const QModelIndex &parent) const
......
......@@ -84,7 +84,13 @@ const KateFileTreePluginSettings &KateFileTreePlugin::settings()
return m_settings;
}
void KateFileTreePlugin::applyConfig(bool shadingEnabled, const QColor &viewShade, const QColor &editShade, bool listMode, int sortRole, bool showFullPath)
void KateFileTreePlugin::applyConfig(bool shadingEnabled,
const QColor &viewShade,
const QColor &editShade,
bool listMode,
int sortRole,
bool showFullPath,
bool showCloseButton)
{
// save to settings
m_settings.setShadingEnabled(shadingEnabled);
......@@ -94,6 +100,7 @@ void KateFileTreePlugin::applyConfig(bool shadingEnabled, const QColor &viewShad
m_settings.setListMode(listMode);
m_settings.setSortRole(sortRole);
m_settings.setShowFullPathOnRoots(showFullPath);
m_settings.setShowCloseButton(showCloseButton);
m_settings.save();
// update views
......@@ -105,6 +112,7 @@ void KateFileTreePlugin::applyConfig(bool shadingEnabled, const QColor &viewShad
view->setListMode(listMode);
view->proxy()->setSortRole(sortRole);
view->model()->setShowFullPathOnRoots(showFullPath);
view->tree()->setShowCloseButton(showCloseButton);
}
}
......@@ -139,6 +147,7 @@ KateFileTreePluginView::KateFileTreePluginView(KTextEditor::MainWindow *mainWind
// create filetree
m_fileTree = new KateFileTree(m_toolView);
m_fileTree->setSortingEnabled(true);
m_fileTree->setShowCloseButton(m_plug->settings().showCloseButton());
connect(m_fileTree, &KateFileTree::activateDocument, this, &KateFileTreePluginView::activateDocument);
connect(m_fileTree, &KateFileTree::viewModeChanged, this, &KateFileTreePluginView::viewModeChanged);
......@@ -315,7 +324,7 @@ void KateFileTreePluginView::viewChanged(KTextEditor::View *)
// update the model on which doc is active
m_documentModel->documentActivated(doc);
m_fileTree->selectionModel()->setCurrentIndex(index, QItemSelectionModel::ClearAndSelect);
m_fileTree->selectionModel()->setCurrentIndex(index, QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
m_fileTree->scrollTo(index);
......
......@@ -46,7 +46,7 @@ public:
const KateFileTreePluginSettings &settings();
void applyConfig(bool shadingEnabled, const QColor &viewShade, const QColor &editShade, bool listMode, int sortRole, bool showFulPath);
void applyConfig(bool shadingEnabled, const QColor &viewShade, const QColor &editShade, bool listMode, int sortRole, bool showFulPath, bool closeButton);
public Q_SLOTS:
void viewDestroyed(QObject *view);
......
......@@ -25,6 +25,8 @@ KateFileTreePluginSettings::KateFileTreePluginSettings()
m_sortRole = m_group.readEntry("sortRole", int(Qt::DisplayRole));
m_showFullPathOnRoots = m_group.readEntry("showFullPathOnRoots", false);
m_showCloseButton = m_group.readEntry("showCloseButton", true);
}
void KateFileTreePluginSettings::save()
......@@ -35,6 +37,7 @@ void KateFileTreePluginSettings::save()
m_group.writeEntry("listMode", m_listMode);
m_group.writeEntry("sortRole", m_sortRole);
m_group.writeEntry("showFullPathOnRoots", m_showFullPathOnRoots);
m_group.writeEntry("showCloseButton", m_showCloseButton);
m_group.sync();
}
......@@ -98,3 +101,13 @@ void KateFileTreePluginSettings::setShowFullPathOnRoots(bool s)
{
m_showFullPathOnRoots = s;
}
bool KateFileTreePluginSettings::showCloseButton() const
{
return m_showCloseButton;
}
void KateFileTreePluginSettings::setShowCloseButton(bool s)
{
m_showCloseButton = s;
}
......@@ -36,6 +36,9 @@ public:
bool showFullPathOnRoots() const;
void setShowFullPathOnRoots(bool);
bool showCloseButton() const;
void setShowCloseButton(bool);
private:
KConfigGroup m_group;
......@@ -47,6 +50,7 @@ private:
int m_sortRole;
bool m_showFullPathOnRoots;
bool m_showCloseButton;
};
#endif // KATE_FILETREE_PLUGIN_H
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment