#include "lts_tool_bar_area.h" #include "ui_lts_tool_bar_area.h" #include "lts_drag_icon.h" #include #include #include #include #include #include #include #include namespace LtsWidgets { void ShortCut::clear() { name.clear(); identifier.clear(); icon = nullptr; shortcut->disconnect(icon); } LtsToolBarArea::LtsToolBarArea(QWidget *parent) : QWidget(parent) , m_dummyLabel(nullptr) , m_ui(new Ui::LtsToolBarArea) { m_ui->setupUi(this); setAcceptDrops(true); clearToolBarArea(); for(int i=Qt::Key_F5; i<=Qt::Key_F12; ++i) { ShortCut sc(this); sc.shortcut->setKey(QKeySequence(i)); sc.key = i; m_functionKeys.insert(i, sc); } } LtsToolBarArea::~LtsToolBarArea() { delete m_ui; } void LtsToolBarArea::addMenuEntry(LtsDragIcon *entry, LtsDragIcon *after) { if (!entry) return; if (m_foreignButtonMap.contains(entry)) return; delete m_dummyLabel; m_dummyLabel = nullptr; bool isInternalMove = false; LtsDragIcon *foreignDragIcon = entry; for (int i = 0; i < m_buttonMap.size(); ++i) { if (m_buttonMap.at(i) == entry) { isInternalMove = true; if (after == nullptr) return; m_buttonMap.removeAt(i); foreignDragIcon = m_foreignButtonMap.at(i); m_foreignButtonMap.removeAt(i); m_ui->layToolbar->removeWidget(entry); break; } } int index = m_buttonMap.size(); for(int i = 0; i < m_buttonMap.size(); ++i) { if (m_buttonMap.at(i) == after || m_foreignButtonMap.at(i) == after) { index = i + 1; break; } } LtsDragIcon *newAction = entry; if (!isInternalMove) { newAction = new LtsDragIcon(this); newAction->setIcon(entry->icon()); newAction->setText(entry->text()); newAction->setContext(entry->context()); if (!entry->toolTip().isEmpty()) newAction->setToolTip(entry->toolTip()); newAction->setIconSize(ButtonSize::eDefault); newAction->setButtonStyle(ButtonStyle::eIconOnly); newAction->setIdentifier(entry->identifier()); connect(newAction, &LtsDragIcon::clicked, entry, &LtsDragIcon::clicked); connect(entry, &LtsDragIcon::destroyed, newAction, [this, newAction]() { m_buttonMap.removeAll(newAction); newAction->deleteLater(); }); newAction->setVisible(!entry->text().isEmpty()); } newAction->setContextMenuPolicy(Qt::CustomContextMenu); connect(newAction, &LtsDragIcon::customContextMenuRequested, this, &LtsToolBarArea::showContextMenu); m_foreignButtonMap.insert(index, foreignDragIcon); m_buttonMap.insert(index, newAction); connect(foreignDragIcon, &LtsDragIcon::enabledChanged, newAction, &LtsDragIcon::setEnabled); m_ui->layToolbar->insertWidget(index + 1, newAction); saveSettings(); } void LtsToolBarArea::clearToolBarArea() { qDeleteAll(m_buttonMap); m_buttonMap.clear(); m_foreignButtonMap.clear(); for(ShortCut &item : m_functionKeys) item.clear(); delete m_dummyLabel; m_dummyLabel = new QLabel(tr("Drop menu entry here"), this); m_ui->layToolbar->addWidget(m_dummyLabel); } void LtsToolBarArea::setMenuEntries(const QVector &actions) { clearToolBarArea(); for (LtsDragIcon* ico : actions) addMenuEntry(ico); } QStringList LtsToolBarArea::menuEntries() const { QStringList ret; ret.reserve(m_foreignButtonMap.size()); for (LtsDragIcon *dragIcon : m_foreignButtonMap) ret << dragIcon->identifier(); return ret; } void LtsToolBarArea::dragEnterEvent(QDragEnterEvent *event) { LtsDragIcon *source = qobject_cast(event->source()); if (source) { if (!m_foreignButtonMap.contains(source)) { event->setDropAction(Qt::MoveAction); event->accept(); } } } void LtsToolBarArea::dropEvent(QDropEvent *event) { LtsDragIcon *source = qobject_cast(event->source()); LtsDragIcon *after = nullptr; QWidget *w = childAt(event->pos()); while (w) { after = qobject_cast(w); if (after) break; w = w->parentWidget(); } addMenuEntry(source, after); } QMenu *LtsToolBarArea::functionKeyMenu(QWidget *parent) { QMenu* menu = new QMenu(parent); menu->setTitle(tr("Function Key")); QAction *act; for(auto val = m_functionKeys.begin(); val != m_functionKeys.end(); ++val) { ShortCut &shorty = val.value(); act = menu->addAction(QStringLiteral("%1%2").arg(shorty.keyName(), !shorty.name.isEmpty() ? QStringLiteral(" - %1").arg(shorty.name) : QString())); act->setData(shorty.shortcut->key()[0]); connect(act, &QAction::triggered, this, &LtsToolBarArea::functionKeyMenuTriggered); } menu->addSeparator(); act = menu->addAction(tr("Remove Function Key")); act->setData(-1); connect(act, &QAction::triggered, this, &LtsToolBarArea::functionKeyMenuTriggered); return menu; } void LtsToolBarArea::functionKeyMenuTriggered() { QAction* act = qobject_cast(sender()); if (!act) return; int fkey = act->data().toInt(); if (fkey == -1) removeFunctionKey(m_currentIdentifierForContextMenue); else if (fkey > 0) setFunctionKey(fkey, m_currentIdentifierForContextMenue); } bool LtsToolBarArea::removeFunctionKey(const QString &action) { if (action.isEmpty()) return true; for(auto val = m_functionKeys.begin(); val != m_functionKeys.end(); ++val) { ShortCut &itShortcut = val.value(); if (itShortcut.identifier == action) { itShortcut.icon->removeModification(); itShortcut.clear(); break; } } saveSettings(); return true; } bool LtsToolBarArea::setFunctionKey(int fkey, const QString &action) { if (fkey < Qt::Key_F5 || fkey > Qt::Key_F12) return false; LtsDragIcon *icon = nullptr; for (LtsDragIcon *ic : qAsConst(m_buttonMap)) { if (ic->identifier() == action) { icon = ic; break; } } if (!icon) return false; ShortCut &shorty = m_functionKeys[fkey]; for (LtsDragIcon *ic : qAsConst(m_buttonMap)) if (ic->identifier() == shorty.identifier) ic->removeModification(); for(auto val = m_functionKeys.begin(); val != m_functionKeys.end(); ++val) { ShortCut &itShortcut = val.value(); if (itShortcut.identifier == action) { itShortcut.clear(); break; } } shorty.clear(); shorty.identifier = icon->identifier(); shorty.name = icon->text(); shorty.icon = icon; shorty.icon->setModification(shorty.keyName()); connect(shorty.shortcut, &QShortcut::activated, icon, &LtsDragIcon::clicked); saveSettings(); return true; } QMap LtsToolBarArea::functionKeys() const { QMap retVal; for(auto val = m_functionKeys.begin(); val != m_functionKeys.end(); ++val) { const ShortCut &itShortcut = val.value(); if (!itShortcut.identifier.isEmpty()) retVal[itShortcut.key] = itShortcut.identifier; } return retVal; } void LtsToolBarArea::removeMenuAction(LtsDragIcon *icon) { for(auto val = m_functionKeys.begin(); val != m_functionKeys.end(); ++val) { ShortCut &itShortcut = val.value(); if (itShortcut.identifier == icon->identifier()) itShortcut.clear(); } for (LtsDragIcon *ic : qAsConst(m_foreignButtonMap)) { if(ic->identifier() == icon->identifier()) { m_foreignButtonMap.removeAll(ic); break; } } m_buttonMap.removeAll(icon); m_ui->layToolbar->removeWidget(icon); delete icon; saveSettings(); } void LtsToolBarArea::showContextMenu(const QPoint &pos) { QPointer icon = qobject_cast(sender()); if (!icon) return; const QPoint &globalPos = icon->mapToGlobal(pos); QMenu *contextMenu = new QMenu(this); contextMenu->setAttribute(Qt::WA_DeleteOnClose); m_currentIdentifierForContextMenue = icon->identifier(); connect(contextMenu, &QObject::destroyed, this, [this](){ m_currentIdentifierForContextMenue.clear(); }); QAction *delAction = contextMenu->addAction(tr("Remove")); connect(delAction, &QAction::triggered, this, [this, icon] () { if (icon) removeMenuAction(icon); }); QMenu *fkeyMenu = functionKeyMenu(contextMenu); contextMenu->addMenu(fkeyMenu); contextMenu->popup(globalPos); } void LtsToolBarArea::saveSettings() { } }