118 lines
3.2 KiB
C++
118 lines
3.2 KiB
C++
#include "ULineEditMenu.h"
|
|
|
|
#include <QPainter>
|
|
#include <QResizeEvent>
|
|
#include <QApplication>
|
|
|
|
#include "ULineEdit.h"
|
|
|
|
namespace
|
|
{
|
|
const QStringList MENU_LIST = QStringList() << "Clear" << "Copy" << "Paste";
|
|
const QSize Menu_SIZE = QSize(126, 40);
|
|
const int TRIANGLE_WIDTH = 5;
|
|
const int TRIANGLE_HEIGHT = 5;
|
|
}
|
|
|
|
ULineEditMenu* ULineEditMenu::getInstance()
|
|
{
|
|
static ULineEditMenu instance;
|
|
return &instance;
|
|
}
|
|
|
|
ULineEditMenu::ULineEditMenu(QWidget* aParent)
|
|
: QWidget(aParent)
|
|
, mMenuList(new QListWidget(this))
|
|
, mCurrentEdit(nullptr)
|
|
{
|
|
setObjectName("EditMenu");
|
|
setGeometry(0, 0, Menu_SIZE.width(), Menu_SIZE.height());
|
|
setAttribute(Qt::WA_TranslucentBackground, true);
|
|
setWindowFlags(Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint | Qt::Tool | Qt::WindowDoesNotAcceptFocus);
|
|
initializeMenuList();
|
|
connect(mMenuList, &QListWidget::itemClicked, this, &ULineEditMenu::handleMenuItemClicked);
|
|
connect(qApp, &QApplication::aboutToQuit, [this]()
|
|
{
|
|
hide();
|
|
});
|
|
}
|
|
|
|
ULineEditMenu::~ULineEditMenu()
|
|
{
|
|
}
|
|
|
|
void ULineEditMenu::initializeMenuList()
|
|
{
|
|
mMenuList->addItems(MENU_LIST);
|
|
mMenuList->setViewMode(QListView::ListMode);
|
|
mMenuList->setFlow(QListView::LeftToRight);
|
|
mMenuList->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
|
mMenuList->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
|
mMenuList->setHorizontalScrollMode(QListWidget::ScrollPerPixel);
|
|
// QString qss = "QListWidget::Item:hover { background: #006dc7; color: white; }"
|
|
// "QListWidget {background:qlineargradient(spread:pad, x1:0.50, y1:0, x2:0.5, y2:1, stop:0 #505050, stop:1 #606060);font: 10pt; outline: none; border:1px solid #00000000; color: white; }";
|
|
// mMenuList->setStyleSheet(qss);
|
|
}
|
|
|
|
void ULineEditMenu::resizeEvent(QResizeEvent* aEvent)
|
|
{
|
|
if (nullptr == aEvent)
|
|
{
|
|
return;
|
|
}
|
|
QSize size = aEvent->size();
|
|
mMenuList->setGeometry(5, 5, size.width() - 10, size.height() - 15);
|
|
}
|
|
|
|
void ULineEditMenu::paintEvent(QPaintEvent* aEvent)
|
|
{
|
|
QPainter painter(this);
|
|
QLinearGradient linearGradient(0.5, 0, 0.5, 1);
|
|
linearGradient.setColorAt(0, "#505050");
|
|
linearGradient.setColorAt(1, "#606060");
|
|
|
|
QBrush brush(linearGradient);
|
|
painter.setBrush(brush);
|
|
painter.setPen(Qt::NoPen);
|
|
|
|
QRect rect = geometry();
|
|
int w = rect.width();
|
|
int h = rect.height();
|
|
|
|
QPolygon triangle;
|
|
triangle.setPoints(3, w / 2, h - 1, w / 2 - TRIANGLE_WIDTH, h - TRIANGLE_HEIGHT, w / 2 + TRIANGLE_WIDTH, h - TRIANGLE_HEIGHT);
|
|
painter.drawRoundedRect(0, 0, w - 1, h - TRIANGLE_HEIGHT, 5, 5);
|
|
painter.drawPolygon(triangle);
|
|
|
|
QWidget::paintEvent(aEvent);
|
|
}
|
|
|
|
void ULineEditMenu::handleMenuItemClicked(QListWidgetItem* aItem)
|
|
{
|
|
QLineEdit* edit = qobject_cast<QLineEdit*>(mCurrentEdit);
|
|
if (nullptr == aItem || nullptr == edit)
|
|
{
|
|
return;
|
|
}
|
|
aItem->setSelected(false);
|
|
if (MENU_LIST.at(0) == aItem->text())
|
|
{
|
|
edit->clear();
|
|
}
|
|
else if (MENU_LIST.at(1) == aItem->text())
|
|
{
|
|
edit->copy();
|
|
}
|
|
else if (MENU_LIST.at(2) == aItem->text())
|
|
{
|
|
edit->paste();
|
|
}
|
|
hide();
|
|
}
|
|
|
|
void ULineEditMenu::setCurrentEdit(QWidget* aEdit)
|
|
{
|
|
mCurrentEdit = aEdit;
|
|
}
|
|
|