diff --git a/src/plugins/codeeditor/mainframe/naveditmainwindow.cpp b/src/plugins/codeeditor/mainframe/naveditmainwindow.cpp index 713ddb0f3..b781347a8 100644 --- a/src/plugins/codeeditor/mainframe/naveditmainwindow.cpp +++ b/src/plugins/codeeditor/mainframe/naveditmainwindow.cpp @@ -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)); diff --git a/src/plugins/core/mainframe/windowkeeper.cpp b/src/plugins/core/mainframe/windowkeeper.cpp index 8be75295e..93981abc5 100644 --- a/src/plugins/core/mainframe/windowkeeper.cpp +++ b/src/plugins/core/mainframe/windowkeeper.cpp @@ -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)); diff --git a/src/plugins/debugger/interface/menumanager.cpp b/src/plugins/debugger/interface/menumanager.cpp index 4a5ad1d27..db58cf5c8 100644 --- a/src/plugins/debugger/interface/menumanager.cpp +++ b/src/plugins/debugger/interface/menumanager.cpp @@ -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")); @@ -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", diff --git a/src/plugins/recent/mainframe/displayitemdelegate.cpp b/src/plugins/recent/mainframe/displayitemdelegate.cpp index 453cfe513..a45dd8b56 100644 --- a/src/plugins/recent/mainframe/displayitemdelegate.cpp +++ b/src/plugins/recent/mainframe/displayitemdelegate.cpp @@ -3,6 +3,7 @@ // SPDX-License-Identifier: GPL-3.0-or-later #include "displayitemdelegate.h" +#include "displayrecentview.h" #include #include @@ -10,11 +11,22 @@ #include #include #include +#include #include #include #include #include +#include +#include +#include +#include + +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) @@ -22,51 +34,26 @@ DisplayItemDelegate::DisplayItemDelegate(QAbstractItemView *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, @@ -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 +{ + 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(); +} diff --git a/src/plugins/recent/mainframe/displayitemdelegate.h b/src/plugins/recent/mainframe/displayitemdelegate.h index 3e7ac9048..1bd9ae0af 100644 --- a/src/plugins/recent/mainframe/displayitemdelegate.h +++ b/src/plugins/recent/mainframe/displayitemdelegate.h @@ -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: @@ -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 diff --git a/src/plugins/recent/mainframe/displayrecentview.cpp b/src/plugins/recent/mainframe/displayrecentview.cpp index df481b6b4..63be5e823 100644 --- a/src/plugins/recent/mainframe/displayrecentview.cpp +++ b/src/plugins/recent/mainframe/displayrecentview.cpp @@ -7,12 +7,14 @@ #include "displayitemdelegate.h" #include +#include + +#include #include #include #include #include -#include #include #define INIT_DATA "{\n \"Projects\":[],\n \"Documents\":[]\n}\n" @@ -57,7 +59,7 @@ QList DisplayRecentView::itemsFromFile() QList 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; @@ -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() @@ -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() diff --git a/src/plugins/recent/mainframe/displayrecentview.h b/src/plugins/recent/mainframe/displayrecentview.h index 1cdb7239e..7055cfa0d 100644 --- a/src/plugins/recent/mainframe/displayrecentview.h +++ b/src/plugins/recent/mainframe/displayrecentview.h @@ -6,23 +6,30 @@ #define DISPLAYRECENTVIEW_H #include -#include +#include + +DWIDGET_USE_NAMESPACE class QStandardItemModel; class QStandardItem; -class DisplayRecentView : public DTK_WIDGET_NAMESPACE::DListView +class DisplayRecentView : public DListView { Q_OBJECT QStringList cache; - QFileIconProvider iconProvider; + DFileIconProvider iconProvider; public: explicit DisplayRecentView(QWidget *parent = nullptr); + ~DisplayRecentView() override; + virtual QString cachePath(); virtual void add(const QString &data); virtual QIcon icon(const QString &data); virtual QString title() = 0; virtual void load(); virtual QList itemsFromFile(); + + void focusOutEvent(QFocusEvent *e) override; + void mousePressEvent(QMouseEvent *e) override; protected: virtual void saveToFile(const QStringList &cache); protected: diff --git a/src/plugins/recent/mainframe/recentdisplay.cpp b/src/plugins/recent/mainframe/recentdisplay.cpp index 3f9be0d42..30ae5428d 100644 --- a/src/plugins/recent/mainframe/recentdisplay.cpp +++ b/src/plugins/recent/mainframe/recentdisplay.cpp @@ -13,6 +13,7 @@ #include #include #include +#include #include #include @@ -55,7 +56,7 @@ class DisplayProView : public DisplayRecentView QString file = projectFile(elemObj, &kitName, &language, &workspace); if (file.isEmpty() || !QFileInfo(file).exists()) continue; - auto rowItem = new QStandardItem (icon(file), file); + auto rowItem = new DStandardItem (icon(file), file); rowItem->setData(kitName, RecentDisplay::ProjectKitName); rowItem->setData(language, RecentDisplay::ProjectLanguage); rowItem->setData(workspace, RecentDisplay::ProjectWorkspace);