Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: DTK Changed-Project and doc list in recent plugin #98

Merged
merged 1 commit into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/plugins/codeeditor/mainframe/naveditmainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@ void NavEditMainWindow::setToolBarButton(QAction *action)
if (!action)
return;
DIconButton *iconBtn = new DIconButton();
iconBtn->setToolTip(action->text());
iconBtn->setIcon(action->icon());
iconBtn->setMinimumSize(QSize(36, 36));
iconBtn->setIconSize(QSize(20, 20));
Expand Down
1 change: 1 addition & 0 deletions src/plugins/core/mainframe/windowkeeper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ void WindowKeeper::createNavIconBtn(const QString &navName, const QString &iconN
DToolButton *toolBtn = new DToolButton;
toolBtn->setCheckable(true);
toolBtn->setChecked(true);
toolBtn->setToolTip(navName);

toolBtn->setIcon(QIcon::fromTheme(iconName));

Expand Down
2 changes: 0 additions & 2 deletions src/plugins/debugger/interface/menumanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ void MenuManager::initialize(WindowService *windowService)
AbstractAction *actionImpl = new AbstractAction(startDebugging.get());
windowService->addAction(MWM_DEBUG, actionImpl);
windowService->addToolBarActionItem("Start Debugging", startDebugging.get(), "Debug");
windowService->addToolBarActionItem("Debugger.Start", startDebugging.get(), "Debug");

#if 0 // not used yet.
detachDebugger.reset(new QAction("Detach Debugger"));
Expand Down Expand Up @@ -68,7 +67,6 @@ void MenuManager::initialize(WindowService *windowService)
actionImpl = new AbstractAction(abortDebugging.get());
windowService->addAction(MWM_DEBUG, actionImpl);
windowService->addToolBarActionItem("abort_debug", abortDebugging.get(), "Debug");
windowService->addToolBarActionItem("Debugger.Stop", abortDebugging.get(), "Debug");

restartDebugging.reset(new QAction(MWMDA_RESTART_DEBUGGING));
ActionManager::getInstance()->registerAction(restartDebugging.get(), "Debug.Restart.Debugging",
Expand Down
191 changes: 148 additions & 43 deletions src/plugins/recent/mainframe/displayitemdelegate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,70 +3,57 @@
// SPDX-License-Identifier: GPL-3.0-or-later

#include "displayitemdelegate.h"
#include "displayrecentview.h"

#include <DApplication>
#include <DGuiApplicationHelper>
#include <DStyle>
#include <DStyleOptionViewItem>
#include <DPaletteHelper>
#include <DSizeMode>
#include <DListView>

#include <QPainter>
#include <QDebug>
#include <QApplication>
#include <QFileInfo>
#include <QTextDocument>
#include <QTextLayout>
#include <QTextBlock>
#include <QAbstractItemView>

inline constexpr int kRectRadius = { 8 };
inline constexpr int kIconWidth = { 30 };
inline constexpr int kIconHeight = { 30 };
inline constexpr int kIconLeftMargin = { 10 };
inline constexpr int kTextLeftMargin = { 8 };

DisplayItemDelegate::DisplayItemDelegate(QAbstractItemView *parent)
: DStyledItemDelegate (parent)
{

}

void DisplayItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
void DisplayItemDelegate::paint(QPainter *painter,
const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
if (index.isValid()) {
painter->save();

int textTopAlign = Qt::AlignmentFlag::AlignTop;
int textBottomAlign = Qt::AlignmentFlag::AlignBottom;
QString filePath = index.data(Qt::DisplayRole).toString();
QString fileName = QFileInfo(filePath).fileName();

QStyleOptionViewItem drawStyle(option);
DStyledItemDelegate::initStyleOption(&drawStyle, index);
painter->setRenderHint(QPainter::Antialiasing);

drawStyle.text = ""; //清空文本绘制
QStyle *pStyle = drawStyle.widget ? drawStyle.widget->style() : DApplication::style();
pStyle->drawControl(DStyle::CE_ItemViewItem, &drawStyle, painter, drawStyle.widget);

QRect iconRect = pStyle->itemPixmapRect(drawStyle.rect, drawStyle.displayAlignment,
drawStyle.icon.pixmap(drawStyle.rect.height()));
QRect titleTextRect = pStyle->itemTextRect(drawStyle.fontMetrics, drawStyle.rect, textTopAlign, true, fileName)
.adjusted(iconRect.width(), 0, 0, 0);
titleTextRect.setWidth(option.rect.width());

QString eliedFileName = drawStyle.fontMetrics.elidedText(fileName, Qt::ElideRight, titleTextRect.width());
pStyle->drawItemText(painter, titleTextRect, drawStyle.displayAlignment, drawStyle.palette, true, eliedFileName);

//缩小字体
QFont nativeFont(drawStyle.font);
nativeFont.setPointSize(drawStyle.font.pointSize() - 1);
nativeFont.setItalic(true);
painter->setFont(nativeFont);

//计算绘制坐标
QRect nativeTextRect = pStyle->itemTextRect(QFontMetrics(nativeFont), drawStyle.rect, textBottomAlign, true, filePath)
.adjusted(iconRect.width(), 0, 0, 0);
nativeTextRect.setWidth(option.rect.width());

QString eliedFilePath = drawStyle.fontMetrics.elidedText(filePath, Qt::ElideRight, nativeTextRect.width());
painter->drawText(nativeTextRect, textBottomAlign, eliedFilePath);
painter->restore();
} else {
if (!index.isValid())
return DStyledItemDelegate::paint(painter, option, index);
}

painter->setRenderHints(painter->renderHints()
| QPainter::Antialiasing
| QPainter::TextAntialiasing
| QPainter::SmoothPixmapTransform);

QStyleOptionViewItem opt = option;
initStyleOption(&opt, index);

paintItemBackground(painter, opt, index);
QRectF iconRect = paintItemIcon(painter, opt, index);
paintItemColumn(painter, opt, index, iconRect);

painter->setOpacity(1);
}

QSize DisplayItemDelegate::sizeHint(const QStyleOptionViewItem &option,
Expand All @@ -77,8 +64,126 @@ QSize DisplayItemDelegate::sizeHint(const QStyleOptionViewItem &option,
if (size.isValid()) {
return size;
} else {
return {option.rect.width(), option.fontMetrics.height() * 2};
return {option.rect.width(), option.fontMetrics.height() * 2 + 5};
}
}
return DStyledItemDelegate::sizeHint(option, index);
}

void DisplayItemDelegate::paintItemBackground(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
N0rthHXD marked this conversation as resolved.
Show resolved Hide resolved
painter->save();

if (option.widget) {
DPalette pl(DPaletteHelper::instance()->palette(option.widget));
QColor baseColor = pl.color(DPalette::ColorGroup::Active, DPalette::ColorType::ItemBackground);
QColor adjustColor = baseColor;

bool isSelected = (option.state & QStyle::State_Selected) && option.showDecorationSelected;

if (isSelected) {
adjustColor = option.palette.color(DPalette::ColorGroup::Active, QPalette::Highlight);
} else if (option.state & QStyle::StateFlag::State_MouseOver) {
// hover color
adjustColor = DGuiApplicationHelper::adjustColor(baseColor, 0, 0, 0, 0, 0, 0, +10);
} else {
// alternately background color
painter->setOpacity(0);
if (index.row() % 2 == 0) {
adjustColor = DGuiApplicationHelper::adjustColor(baseColor, 0, 0, 0, 0, 0, 0, +5);
painter->setOpacity(1);
}
}

// set paint path
QPainterPath path;
path.addRoundedRect(option.rect, kRectRadius, kRectRadius);
painter->fillPath(path, adjustColor);
}

painter->restore();
}

QRectF DisplayItemDelegate::paintItemIcon(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
painter->save();

if (!parent() || !parent()->parent())
return QRect();

bool isEnabled = option.state & QStyle::State_Enabled;

// draw icon
QRectF iconRect = option.rect;
iconRect.setSize(QSize(kIconWidth, kIconHeight));
iconRect.moveTopLeft(QPoint(kIconLeftMargin, qRound(iconRect.top() + (option.rect.bottom() - iconRect.bottom()) / 2)));

// Copy of QStyle::alignedRect
Qt::Alignment alignment = Qt::AlignCenter;
alignment = visualAlignment(painter->layoutDirection(), alignment);
const qreal pixelRatio = painter->device()->devicePixelRatioF();
const QPixmap &px = getIconPixmap(option.icon, iconRect.size().toSize(), pixelRatio, isEnabled ? QIcon::Normal : QIcon::Disabled, QIcon::Off);
qreal x = iconRect.x();
qreal y = iconRect.y();
qreal w = px.width() / px.devicePixelRatio();
qreal h = px.height() / px.devicePixelRatio();
if ((alignment & Qt::AlignVCenter) == Qt::AlignVCenter)
y += (iconRect.size().height() - h) / 2.0;
else if ((alignment & Qt::AlignBottom) == Qt::AlignBottom)
y += iconRect.size().height() - h;
if ((alignment & Qt::AlignRight) == Qt::AlignRight)
x += iconRect.size().width() - w;
else if ((alignment & Qt::AlignHCenter) == Qt::AlignHCenter)
x += (iconRect.size().width() - w) / 2.0;

painter->drawPixmap(qRound(x), qRound(y), px);
painter->restore();

return iconRect;
}

QPixmap DisplayItemDelegate::getIconPixmap(const QIcon &icon, const QSize &size, qreal pixelRatio = 1.0, QIcon::Mode mode, QIcon::State state)
{
if (icon.isNull())
return QPixmap();

// 确保当前参数参入获取图片大小大于0
if (size.width() <= 0 || size.height() <= 0)
return QPixmap();

auto px = icon.pixmap(size, mode, state);
px.setDevicePixelRatio(qApp->devicePixelRatio());

return px;
}

void DisplayItemDelegate::paintItemColumn(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index, const QRectF &iconRect) const
{
painter->save();

bool isSelected = (option.state & QStyle::State_Selected) && option.showDecorationSelected;
if (isSelected)
painter->setPen(option.palette.color(DPalette::ColorGroup::Active, QPalette::HighlightedText));

QString filePath = index.data(Qt::DisplayRole).toString();
QString fileName = QFileInfo(filePath).fileName();
QFontMetrics fm(option.font);

QString elidFilePath;
elidFilePath = fm.elidedText(filePath, Qt::ElideMiddle,
qRound(option.rect.width() - iconRect.width() - kIconLeftMargin * 2));

painter->drawText(option.rect.adjusted(kIconLeftMargin + kIconWidth + kTextLeftMargin, 5, 0, -5),
Qt::AlignLeft | Qt::AlignBottom, elidFilePath);

QString elidFileName;
elidFileName = fm.elidedText(fileName, Qt::ElideMiddle,
qRound(option.rect.width() - iconRect.width() - kIconLeftMargin * 2));
painter->drawText(option.rect.adjusted(kIconLeftMargin + kIconWidth + kTextLeftMargin, 5, 0, -5),
Qt::AlignLeft | Qt::AlignTop, elidFileName);

painter->restore();
}
21 changes: 21 additions & 0 deletions src/plugins/recent/mainframe/displayitemdelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,20 @@ DWIDGET_USE_NAMESPACE
class DisplayItemDelegate : public DStyledItemDelegate
{
public:
static inline Qt::Alignment visualAlignment(Qt::LayoutDirection direction, Qt::Alignment alignment)
{
if (!(alignment & Qt::AlignHorizontal_Mask))
alignment |= Qt::AlignLeft;
if (!(alignment & Qt::AlignAbsolute) && (alignment & (Qt::AlignLeft | Qt::AlignRight))) {
if (direction == Qt::RightToLeft)
alignment ^= (Qt::AlignLeft | Qt::AlignRight);
alignment |= Qt::AlignAbsolute;
}
return alignment;
}
static QPixmap getIconPixmap(const QIcon &icon, const QSize &size, qreal pixelRatio,
QIcon::Mode mode = QIcon::Normal, QIcon::State state = QIcon::Off);

explicit DisplayItemDelegate(QAbstractItemView *parent = nullptr);

protected:
Expand All @@ -20,6 +34,13 @@ class DisplayItemDelegate : public DStyledItemDelegate

QSize sizeHint(const QStyleOptionViewItem &option,
const QModelIndex &index) const override;

void paintItemBackground(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index) const;
QRectF paintItemIcon(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index) const;
void paintItemColumn(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index, const QRectF &iconRect) const;
};

#endif // DISPLAYITEMDELEGATE_H
59 changes: 44 additions & 15 deletions src/plugins/recent/mainframe/displayrecentview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
#include "displayitemdelegate.h"

#include <DStyledItemDelegate>
#include <DStandardItem>

#include <QToolTip>

#include <QDir>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <QStandardItem>
#include <QStandardItemModel>

#define INIT_DATA "{\n \"Projects\":[],\n \"Documents\":[]\n}\n"
Expand Down Expand Up @@ -57,7 +59,7 @@ QList<QStandardItem *> DisplayRecentView::itemsFromFile()
QList<QStandardItem*> result;
for (auto one : array) {
QString filePath(one.toString());
auto rowItem = new QStandardItem (icon(filePath), filePath);
auto rowItem = new DStandardItem (icon(filePath), filePath);
rowItem->setToolTip(filePath);
if (!cache.contains(filePath)) {
cache << filePath;
Expand All @@ -71,13 +73,46 @@ DisplayRecentView::DisplayRecentView(QWidget *parent)
: DListView (parent)
, cache({})
, model(new QStandardItemModel(this))
{
setDragDropMode(QAbstractItemView::NoDragDrop);
setSelectionMode(QAbstractItemView::SingleSelection);
setSelectionBehavior(QAbstractItemView::SelectRows);
setEditTriggers(QListView::NoEditTriggers);
setTextElideMode(Qt::ElideMiddle);
setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setAlternatingRowColors(false);
setSelectionRectVisible(true);
setMouseTracking(true);

setUniformItemSizes(true);
setResizeMode(Fixed);
setOrientation(QListView::TopToBottom, false);
setFrameStyle(QFrame::NoFrame);
setSpacing(0);
setContentsMargins(0, 0, 0, 0);

setModel(model);
setItemDelegate(new DisplayItemDelegate(this));
}

DisplayRecentView::~DisplayRecentView()
{
DListView::setModel(model);
DListView::setEditTriggers(DListView::NoEditTriggers);
DListView::setItemDelegate(new DisplayItemDelegate(this));
DListView::setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
DListView::setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
DListView::setSelectionMode(QAbstractItemView::NoSelection);

}

void DisplayRecentView::focusOutEvent(QFocusEvent *e)
{
DListView::clearSelection();

DListView::focusOutEvent(e);
}

void DisplayRecentView::mousePressEvent(QMouseEvent *e)
{
if (e->button() != Qt::LeftButton)
return;
return DListView::mousePressEvent(e);
}

QString DisplayRecentView::cachePath()
Expand All @@ -100,13 +135,7 @@ void DisplayRecentView::add(const QString &data)
QIcon DisplayRecentView::icon(const QString &data)
{
QFileInfo info(data);
if (info.isFile()) {
return iconProvider.icon(QFileIconProvider::File);
}
if (info.isDir()) {
return iconProvider.icon(QFileIconProvider::Folder);
}
return QIcon();
return iconProvider.icon(info);
}

void DisplayRecentView::load()
Expand Down
Loading