Commit 3686113c authored by Waqar Ahmed's avatar Waqar Ahmed
Browse files

Revise Quickopen UI and make it single row, single column

parent 7c204923
......@@ -17,7 +17,6 @@
#include <KAboutData>
#include <KActionCollection>
//#include <KLineEdit>
#include <KLocalizedString>
#include <KPluginFactory>
......@@ -144,7 +143,7 @@ public:
}
const auto pathFontsize = option.font.pointSize();
doc.setHtml(QStringLiteral("<span style=\"font-size: %1pt;\">").arg(pathFontsize + 1) + name + QStringLiteral("</span>") + QStringLiteral("<br>") + QStringLiteral("<span style=\"color: gray; font-size: %1pt;\">").arg(pathFontsize) + path + QStringLiteral("</span>"));
doc.setHtml(QStringLiteral("<span style=\"font-size: %1pt;\">").arg(pathFontsize + 1) + name + QStringLiteral("</span>") + QStringLiteral(" &nbsp;") + QStringLiteral("<span style=\"color: gray; font-size: %1pt;\">").arg(pathFontsize) + path + QStringLiteral("</span>"));
doc.setDocumentMargin(2);
painter->save();
......@@ -160,7 +159,10 @@ public:
options.widget->style()->drawControl(QStyle::CE_ItemViewItem, &options, painter, options.widget);
// draw text
painter->translate(option.rect.x() + 5, option.rect.y());
painter->translate(option.rect.x(), option.rect.y());
if (index.column() == 0) {
painter->translate(25, 0);
}
doc.drawContents(painter);
painter->restore();
......@@ -183,21 +185,21 @@ private:
// QAbstractItemDelegate interface
public:
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override
{
QSize size = this->QStyledItemDelegate::sizeHint(option, index);
static int height = -1;
if (height > -1) {
size.setHeight(height);
return size;
}
QFontMetrics metrics(option.font);
QRect outRect = metrics.boundingRect(QRect(QPoint(0, 0), size), Qt::AlignLeft, option.text);
height = outRect.height() * 2 + 4;
size.setHeight(outRect.height() * 2 + 4);
return size;
}
// QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override
// {
// QSize size = this->QStyledItemDelegate::sizeHint(option, index);
// static int height = -1;
// if (height > -1) {
// size.setHeight(height);
// return size;
// }
// QFontMetrics metrics(option.font);
// QRect outRect = metrics.boundingRect(QRect(QPoint(0, 0), size), Qt::AlignLeft, option.text);
// height = outRect.height() * 2 + 4;
// size.setHeight(outRect.height() * 2 + 4);
// return size;
// }
};
Q_DECLARE_METATYPE(QPointer<KTextEditor::Document>)
......@@ -214,7 +216,6 @@ KateQuickOpen::KateQuickOpen(KateMainWindow *mainWindow)
m_inputLine = new QuickOpenLineEdit(this);
setFocusProxy(m_inputLine);
m_inputLine->setPlaceholderText(i18n("Quick Open Search"));
layout->addWidget(m_inputLine);
......
......@@ -6,37 +6,27 @@
*/
#include "katequickopenlineedit.h"
#include <QStyleOptionFocusRect>
#include <QStylePainter>
#include <QtDebug>
#include <QWindow>
#include <QContextMenuEvent>
#include <QMenu>
SwitchModeButton::SwitchModeButton(QWidget *parent)
: QAbstractButton(parent)
{
setCursor(Qt::CursorShape::PointingHandCursor);
setFocusPolicy(Qt::NoFocus);
m_icon = QIcon::fromTheme(QStringLiteral("settings-configure"));
}
#include <KLocalizedString>
void SwitchModeButton::paintEvent(QPaintEvent *)
QuickOpenLineEdit::QuickOpenLineEdit(QWidget* parent)
: QLineEdit(parent)
{
const QPixmap iconPixmap = m_icon.pixmap(sizeHint(), QIcon::Normal);
QStylePainter painter(this);
QRect pixmapRect(QPoint(0,0), sizeHint());
pixmapRect.moveCenter(rect().center());
painter.drawPixmap(pixmapRect, iconPixmap);
setPlaceholderText(i18n("Quick Open Search (configure via context menu)"));
}
QuickOpenLineEdit::QuickOpenLineEdit(QWidget* parent)
: QLineEdit(parent)
void QuickOpenLineEdit::contextMenuEvent(QContextMenuEvent *event)
{
m_button = new SwitchModeButton(this);
m_menu = new QMenu(this);
// standard stuff like copy & paste...
QMenu *contextMenu = createStandardContextMenu();
auto act = m_menu->addAction(QStringLiteral("Filter By Path"));
QMenu* menu = new QMenu(QStringLiteral("Filter..."));
// our configuration actions
menu->addSeparator();
auto act = menu->addAction(QStringLiteral("Filter By Path"));
act->setCheckable(true);
connect(act, &QAction::toggled, this, [this](bool checked){
m_mode.setFlag(FilterMode::FilterByPath, checked);
......@@ -44,7 +34,7 @@ QuickOpenLineEdit::QuickOpenLineEdit(QWidget* parent)
});
act->setChecked(true);
act = m_menu->addAction(QStringLiteral("Filter By Name"));
act = menu->addAction(QStringLiteral("Filter By Name"));
act->setCheckable(true);
connect(act, &QAction::toggled, this, [this](bool checked){
m_mode.setFlag(FilterMode::FilterByName, checked);
......@@ -52,21 +42,22 @@ QuickOpenLineEdit::QuickOpenLineEdit(QWidget* parent)
});
act->setChecked(true);
m_menu->addSeparator();
menu->addSeparator();
QActionGroup* actGp = new QActionGroup(this);
actGp->setExclusionPolicy(QActionGroup::ExclusionPolicy::Exclusive);
act = m_menu->addAction(QStringLiteral("All Projects"));
act = menu->addAction(QStringLiteral("All Projects"));
act->setCheckable(true);
connect(act, &QAction::toggled, this, [this](bool checked){
if (checked)
emit listModeChanged(KateQuickOpenModelList::AllProjects);
});
act->setChecked(true);
actGp->addAction(act);
act = m_menu->addAction(QStringLiteral("Current Project"));
act = menu->addAction(QStringLiteral("Current Project"));
connect(act, &QAction::toggled, this, [this](bool checked){
if (checked)
emit listModeChanged(KateQuickOpenModelList::CurrentProject);
......@@ -75,26 +66,8 @@ QuickOpenLineEdit::QuickOpenLineEdit(QWidget* parent)
actGp->addAction(act);
connect(m_button, &SwitchModeButton::clicked, this, &QuickOpenLineEdit::openMenu);
}
void QuickOpenLineEdit::updateViewGeometry()
{
QMargins margins(0, 0, m_button->sizeHint().width(), 0);
setTextMargins(margins);
auto iconOffset = textMargins().right() + 8;
m_button->setGeometry(rect().adjusted(width() - iconOffset, 0, 0, 0));
}
contextMenu->addMenu(menu);
void QuickOpenLineEdit::resizeEvent(QResizeEvent*)
{
updateViewGeometry();
}
void QuickOpenLineEdit::openMenu()
{
int y = pos().y() + height();
int x = pos().x() + width() - m_menu->sizeHint().width();
m_menu->exec(mapToGlobal(QPoint(x, y)));
contextMenu->exec(event->globalPos());
delete contextMenu;
}
......@@ -8,29 +8,9 @@
#define QUICKOPENLINEEDIT_H
#include <QLineEdit>
#include <QAbstractButton>
#include <QIcon>
#include "katequickopenmodel.h"
class SwitchModeButton : public QAbstractButton
{
public:
explicit SwitchModeButton(QWidget *parent);
QSize sizeHint() const override
{
return m_icon.actualSize(QSize(24, 24));
}
protected:
void paintEvent(QPaintEvent *) override;
private:
QIcon m_icon;
};
enum FilterMode : uint8_t
{
FilterByName = 0x01, /* By File Name */
......@@ -50,19 +30,12 @@ public:
return m_mode;
}
void updateViewGeometry();
protected:
void resizeEvent(QResizeEvent *event) override;
void contextMenuEvent(QContextMenuEvent *event) override;
private:
SwitchModeButton* m_button;
QMenu* m_menu;
FilterModes m_mode = (FilterMode)(FilterMode::FilterByName | FilterMode::FilterByPath);
private Q_SLOTS:
void openMenu();
Q_SIGNALS:
void filterModeChanged(FilterModes mode);
void listModeChanged(KateQuickOpenModelList mode);
......
......@@ -14,6 +14,8 @@
#include <ktexteditor/document.h>
#include <ktexteditor/view.h>
#include <QMimeDatabase>
KateQuickOpenModel::KateQuickOpenModel(KateMainWindow *mainWindow, QObject *parent)
: QAbstractTableModel(parent)
, m_mainWindow(mainWindow)
......@@ -40,10 +42,6 @@ QVariant KateQuickOpenModel::data(const QModelIndex &idx, int role) const
return {};
}
if (role != Qt::DisplayRole && role != Qt::FontRole && role != Qt::UserRole && role != Role::Score) {
return {};
}
auto entry = m_modelEntries.at(idx.row());
if (role == Qt::DisplayRole) {
switch (idx.column()) {
......@@ -56,6 +54,8 @@ QVariant KateQuickOpenModel::data(const QModelIndex &idx, int role) const
font.setBold(true);
return font;
}
} else if (role == Qt::DecorationRole) {
return QIcon::fromTheme(QMimeDatabase().mimeTypeForFile(entry.fileName, QMimeDatabase::MatchExtension).iconName());
} else if (role == Qt::UserRole) {
return entry.url;
} else if (role == Role::Score) {
......@@ -88,21 +88,23 @@ void KateQuickOpenModel::refresh()
QVector<ModelEntry> allDocuments;
allDocuments.reserve(sortedViews.size() + openDocs.size() + projectDocs.size());
QMimeDatabase mdb;
size_t sort_id = static_cast<size_t>(-1);
for (auto *view : qAsConst(sortedViews)) {
auto doc = view->document();
allDocuments.push_back({doc->url(), doc->documentName(), doc->url().toDisplayString(QUrl::NormalizePathSegments | QUrl::PreferLocalFile).remove(projectBase), true, sort_id--, -1});
allDocuments.push_back({doc->url(), doc->documentName(), doc->url().toDisplayString(QUrl::NormalizePathSegments | QUrl::PreferLocalFile).remove(projectBase).remove(QStringLiteral("/") + doc->documentName()), true, sort_id--, -1});
}
for (auto *doc : qAsConst(openDocs)) {
const auto normalizedUrl = doc->url().toString(QUrl::NormalizePathSegments | QUrl::PreferLocalFile).remove(projectBase);
const auto normalizedUrl = doc->url().toString(QUrl::NormalizePathSegments | QUrl::PreferLocalFile).remove(projectBase).remove(QStringLiteral("/") + doc->documentName());
allDocuments.push_back({doc->url(), doc->documentName(), normalizedUrl, true, 0, -1});
}
for (const auto &file : qAsConst(projectDocs)) {
QFileInfo fi(file);
const auto localFile = QUrl::fromLocalFile(fi.absoluteFilePath());
allDocuments.push_back({localFile, fi.fileName(), fi.filePath().remove(projectBase), false, 0, -1});
allDocuments.push_back({localFile, fi.fileName(), fi.filePath().remove(projectBase).remove(QStringLiteral("/") + fi.fileName()), false, 0, -1});
}
/** Sort the arrays by filePath. */
......@@ -112,7 +114,7 @@ void KateQuickOpenModel::refresh()
* Note that the stable_sort above guarantees that the items that the
* bold/sort_id fields of the items added first are correctly preserved.
*/
allDocuments.erase(std::unique(allDocuments.begin(), allDocuments.end(), [](const ModelEntry &a, const ModelEntry &b) { return a.filePath == b.filePath; }), std::end(allDocuments));
allDocuments.erase(std::unique(allDocuments.begin(), allDocuments.end(), [](const ModelEntry &a, const ModelEntry &b) { return a.url == b.url; }), std::end(allDocuments));
/** sort the arrays via boldness (open or not */
std::stable_sort(std::begin(allDocuments), std::end(allDocuments), [](const ModelEntry &a, const ModelEntry &b) {
......
......@@ -12,6 +12,7 @@
#include <QVariant>
#include <QVector>
#include <QUrl>
#include <QIcon>
class KateMainWindow;
......
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