Skip to content

Commit

Permalink
Actions (#10)
Browse files Browse the repository at this point in the history
* added actions of context-menu's to the quick-access-list
  • Loading branch information
TheCrazyT authored Jan 14, 2023
1 parent 4f73d75 commit 814f89a
Showing 1 changed file with 59 additions and 13 deletions.
72 changes: 59 additions & 13 deletions quickaccess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,39 @@
#include <QStringListModel>
#include <QCompleter>

static void parseMenu(QAction* menuAction, int level, QStringList& list){
static void parseMenu(QAction* menuAction, int level, QStringList& list, QString parentName, boolean addParentPath) {
QMenu* menu = menuAction->menu();
if(menu != nullptr){
foreach(QAction* action, menu->actions()){
foreach(QAction* action, menu->actions()) {
if(action != nullptr){
dbgf("%0*d%s\n", level, 0, action->text().toStdString().c_str());
parseMenu(action, level+1, list);
parentName = menuAction->text();
parseMenu(action, level+1, list, parentName, addParentPath);
}
}
}else{
list.append(menuAction->text().replace("&",""));
char buf[1024];
if(addParentPath){
sprintf(&buf[0],"%s -> %s", parentName.replace("&","").toStdString().c_str(), menuAction->text().replace("&","").toStdString().c_str());
}else{
sprintf(&buf[0],"%s", menuAction->text().replace("&","").toStdString().c_str());
}
list.append(buf);
}
}

static void parseWidget(QMainWindow* mwnd, QStringList& actionList) {
foreach(QWidget* widget, mwnd->findChildren<QWidget*>(QString(), Qt::FindChildrenRecursively)) {
if(widget != mwnd->menuBar()) {
foreach(QAction* action, widget->actions()) {
dbg(action->text().toStdString().c_str());
QString parentName = widget->windowTitle();
parseMenu(action, 1, actionList, parentName, true);
}
}
}
}

QuickAccess::QuickAccess(QMainWindow* mwnd) : QDialog(mwnd){
txt = new QLineEdit();
auto layout = new QGridLayout();
Expand All @@ -40,8 +59,13 @@ QuickAccess::QuickAccess(QMainWindow* mwnd) : QDialog(mwnd){
dbg("Mainwindow valid.");
foreach (QAction* action, menuBar->actions()){
dbg(action->text().toStdString().c_str());
parseMenu(action, 1, actionList);
parseMenu(action, 1, actionList, QString(), false);
}
foreach (QAction* action, mwnd->actions()){
dbg(action->text().toStdString().c_str());
parseMenu(action, 1, actionList, QString(), false);
}
parseWidget(mwnd, actionList);
}

auto completer = new QCompleter(txt);
Expand All @@ -54,7 +78,7 @@ QuickAccess::QuickAccess(QMainWindow* mwnd) : QDialog(mwnd){
txt->setCompleter(completer);
}

void QuickAccess::display(){
void QuickAccess::display() {
show();
raise();
activateWindow();
Expand All @@ -67,34 +91,56 @@ void QuickAccess::display(){
bool findByString(QString txt, QAction* menuAction){
QMenu* menu = menuAction->menu();
if(menu != NULL){
foreach(QAction* action, menu->actions()){
foreach(QAction* action, menu->actions()) {
if(action != NULL){
if(findByString(txt, action)){
if(findByString(txt, action)) {
return true;
}
}
}
}else{
if(menuAction->text().replace("&","").toLower().contains(txt.toLower())){
QString find = txt.toLower();
if(find.contains("->")){
find = find.mid(find.indexOf(" -> ")+4);
}
if(menuAction->text().replace("&","").toLower().contains(find)) {
emit menuAction->trigger();
return true;
}
}
return false;
}

void QuickAccess::txtReturnPressed(){
void QuickAccess::txtReturnPressed() {
dbg("return pressed");

boolean found = false;
if(menuBar != NULL){
dbg("Mainwindow valid.");
foreach(QAction* action, menuBar->actions()){
if(findByString(txt->text(), action)){
if(findByString(txt->text(), action)){
found = true;
break;
}
}
}

if((!found)&&(parent() != NULL)) {
foreach(QAction* action, ((QMainWindow*)parent())->actions()) {
if(findByString(txt->text(), action)){
found = true;
break;
}
}
if(!found) {
foreach(QWidget* widget, ((QMainWindow*)parent())->findChildren<QWidget*>(QString(), Qt::FindChildrenRecursively)) {
foreach(QAction* action, widget->actions()){
if(findByString(txt->text(), action)){
found = true;
break;
}
}
}
}
}
hide();
accept();
}

0 comments on commit 814f89a

Please sign in to comment.