|
| 1 | +#include <QTimer> |
| 2 | +#include <QApplication> |
| 3 | +#include <QMouseEvent> |
| 4 | +#include "ansimenu.h" |
| 5 | + |
| 6 | +/// |
| 7 | +/// \brief AnsiMenu::AnsiMenu |
| 8 | +/// |
| 9 | +AnsiMenu::AnsiMenu(QWidget *parent) |
| 10 | + : QMenu(parent) |
| 11 | +{ |
| 12 | + _actionArabic = new QAction(tr("Arabic"), parent); |
| 13 | + _actionBaltic = new QAction(tr("Baltic"), parent); |
| 14 | + _actionCeltic = new QAction(tr("Celtic"), parent); |
| 15 | + _actionCyrillic = new QAction(tr("Cyrillic"), parent); |
| 16 | + _actionCentralEuropean = new QAction(tr("Central European"), parent); |
| 17 | + _actionChinese = new QAction(tr("Chinese"), parent); |
| 18 | + _actionEasternEuropean = new QAction(tr("Eastern European"), parent); |
| 19 | + _actionGreek = new QAction(tr("Greek"), parent); |
| 20 | + _actionHebrew = new QAction(tr("Hebrew"), parent); |
| 21 | + _actionJapanese = new QAction(tr("Japanese"), parent); |
| 22 | + _actionKorean = new QAction(tr("Korean"), parent); |
| 23 | + _actionThai = new QAction(tr("Thai"), parent); |
| 24 | + _actionTurkish = new QAction(tr("Turkish"), parent); |
| 25 | + _actionWesternEuropean = new QAction(tr("Western European"), parent); |
| 26 | + _actionVietnamese = new QAction(tr("Vietnamese"), parent); |
| 27 | + |
| 28 | + createSubMenu(_actionArabic, { "ISO 8859-6", "Windows-1256" }); |
| 29 | + createSubMenu(_actionBaltic, { "ISO 8859-4", "ISO 8859-13", "Windows-1257" }); |
| 30 | + createSubMenu(_actionCeltic, { "ISO 8859-14" }); |
| 31 | + createSubMenu(_actionCyrillic, { "ISO 8859-5", "KOI8-R", "KOI8-U", "Windows-1251" }); |
| 32 | + createSubMenu(_actionCentralEuropean, { "Windows-1250" }); |
| 33 | + createSubMenu(_actionChinese, { "Big5" }); |
| 34 | + createSubMenu(_actionEasternEuropean, { "ISO 8859-2" }); |
| 35 | + createSubMenu(_actionGreek, { "ISO 8859-7", "Windows-1253" }); |
| 36 | + createSubMenu(_actionHebrew, { "ISO 8859-8", "Windows-1255" }); |
| 37 | + createSubMenu(_actionJapanese, { "Shift-JIS" }); |
| 38 | + createSubMenu(_actionKorean, { "EUC-KR" }); |
| 39 | + createSubMenu(_actionThai, { "TIS-620" }); |
| 40 | + createSubMenu(_actionTurkish, { "ISO 8859-3", "ISO 8859-9", "Windows-1254" }); |
| 41 | + createSubMenu(_actionWesternEuropean, { "ISO 8859-1", "ISO 8859-15", "Windows-1252" }); |
| 42 | + createSubMenu(_actionVietnamese, { "Windows-1258" }); |
| 43 | +} |
| 44 | + |
| 45 | +/// |
| 46 | +/// \brief AnsiMenu::changeEvent |
| 47 | +/// \param event |
| 48 | +/// |
| 49 | +void AnsiMenu::changeEvent(QEvent* event) |
| 50 | +{ |
| 51 | + if (event->type() == QEvent::LanguageChange) |
| 52 | + { |
| 53 | + _actionArabic->setText(tr("Arabic")); |
| 54 | + _actionBaltic->setText(tr("Baltic")); |
| 55 | + _actionCeltic->setText(tr("Celtic")); |
| 56 | + _actionCyrillic->setText(tr("Cyrillic")); |
| 57 | + _actionCentralEuropean->setText(tr("Central European")); |
| 58 | + _actionChinese->setText(tr("Chinese")); |
| 59 | + _actionEasternEuropean->setText(tr("Eastern European")); |
| 60 | + _actionGreek->setText(tr("Greek")); |
| 61 | + _actionHebrew->setText(tr("Hebrew")); |
| 62 | + _actionJapanese->setText(tr("Japanese")); |
| 63 | + _actionKorean->setText(tr("Korean")); |
| 64 | + _actionThai->setText(tr("Thai")); |
| 65 | + _actionTurkish->setText(tr("Turkish")); |
| 66 | + _actionWesternEuropean->setText(tr("Western European")); |
| 67 | + _actionVietnamese->setText(tr("Vietnamese")); |
| 68 | + } |
| 69 | + |
| 70 | + QMenu::changeEvent(event); |
| 71 | +} |
| 72 | + |
| 73 | +/// |
| 74 | +/// \brief AnsiMenu::mouseReleaseEvent |
| 75 | +/// \param event |
| 76 | +/// |
| 77 | +void AnsiMenu::mouseReleaseEvent(QMouseEvent* event) |
| 78 | +{ |
| 79 | + if (event->button() == Qt::LeftButton) |
| 80 | + { |
| 81 | + menuAction()->trigger(); |
| 82 | + closeMenu(); |
| 83 | + } |
| 84 | + |
| 85 | + QMenu::mouseReleaseEvent(event); |
| 86 | +} |
| 87 | + |
| 88 | +/// |
| 89 | +/// \brief AnsiMenu::closeMenu |
| 90 | +/// |
| 91 | +void AnsiMenu::closeMenu() |
| 92 | +{ |
| 93 | + for (auto&& widget : QApplication::topLevelWidgets()) |
| 94 | + { |
| 95 | + auto menu = qobject_cast<QMenu*>(widget); |
| 96 | + if (menu && menu->isVisible() && menu != this) |
| 97 | + menu->close(); |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +/// |
| 102 | +/// \brief AnsiMenu::createSubMenu |
| 103 | +/// \param a |
| 104 | +/// \param encodings |
| 105 | +/// |
| 106 | +void AnsiMenu::createSubMenu(QAction* a, const QStringList& encodings) |
| 107 | +{ |
| 108 | + auto menu = new QMenu(this); |
| 109 | + for(auto&& e : encodings) |
| 110 | + { |
| 111 | + auto action = new QAction(e, a); |
| 112 | + action->setCheckable(true); |
| 113 | + |
| 114 | + menu->addAction(action); |
| 115 | + connect(action, &QAction::triggered, this, [this, action, a](){ |
| 116 | + resetState(); |
| 117 | + a->setChecked(true); |
| 118 | + action->setChecked(true); |
| 119 | + emit codepageSelected(action->text()); |
| 120 | + menuAction()->trigger(); |
| 121 | + }); |
| 122 | + } |
| 123 | + |
| 124 | + a->setCheckable(true); |
| 125 | + a->setMenu(menu); |
| 126 | + |
| 127 | + addAction(a); |
| 128 | +} |
| 129 | + |
| 130 | +/// |
| 131 | +/// \brief AnsiMenu::selectCodepage |
| 132 | +/// \param name |
| 133 | +/// |
| 134 | +void AnsiMenu::selectCodepage(const QString& name) |
| 135 | +{ |
| 136 | + for(auto&& a : actions()) |
| 137 | + { |
| 138 | + a->setChecked(false); |
| 139 | + for(auto&& ma : a->menu()->actions()) |
| 140 | + { |
| 141 | + if(ma->text() == name) |
| 142 | + { |
| 143 | + a->setChecked(true); |
| 144 | + ma->setChecked(true); |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +/// |
| 151 | +/// \brief AnsiMenu::resetState |
| 152 | +/// |
| 153 | +void AnsiMenu::resetState() |
| 154 | +{ |
| 155 | + for(auto&& a : actions()) |
| 156 | + { |
| 157 | + a->setChecked(false); |
| 158 | + for(auto&& ma : a->menu()->actions()) |
| 159 | + ma->setChecked(false); |
| 160 | + } |
| 161 | +} |
0 commit comments