1
mirror of https://invent.kde.org/network/falkon.git synced 2024-12-20 18:56:34 +01:00

Merge pull request #1432 from trollixx/encoding

Refactor encoding menu creation
This commit is contained in:
David Rosca 2014-07-22 22:20:00 +02:00
commit 366cfc60c5
2 changed files with 61 additions and 50 deletions

View File

@ -373,6 +373,38 @@ void BrowserWindow::setupMenu()
connect(inspectorAction, SIGNAL(activated()), this, SLOT(showWebInspector())); connect(inspectorAction, SIGNAL(activated()), this, SLOT(showWebInspector()));
} }
QAction *BrowserWindow::createEncodingAction(const QString &codecName,
const QString &activeCodecName, QMenu *menu)
{
QAction* action = new QAction(codecName, menu);
action->setData(codecName);
action->setCheckable(true);
connect(action, SIGNAL(triggered()), this, SLOT(changeEncoding()));
if (activeCodecName.compare(codecName, Qt::CaseInsensitive) == 0) {
action->setChecked(true);
}
return action;
}
void BrowserWindow::createEncodingSubMenu(const QString &name, QStringList &codecNames, QMenu *menu)
{
if (codecNames.isEmpty()) {
return;
}
/// TODO: Alphanumeric sorting: QCollator (5.2+) or http://www.davekoelle.com/alphanum.html
std::sort(codecNames.begin(), codecNames.end());
QMenu* subMenu = new QMenu(name, menu);
const QString activeCodecName = QWebSettings::globalSettings()->defaultTextEncoding();
foreach (const QString &codecName, codecNames) {
subMenu->addAction(createEncodingAction(codecName, activeCodecName, subMenu));
}
menu->addMenu(subMenu);
}
void BrowserWindow::loadSettings() void BrowserWindow::loadSettings()
{ {
Settings settings; Settings settings;
@ -923,68 +955,43 @@ void BrowserWindow::createSidebarsMenu(QMenu* menu)
void BrowserWindow::createEncodingMenu(QMenu* menu) void BrowserWindow::createEncodingMenu(QMenu* menu)
{ {
QMenu* menuISO = new QMenu("ISO", this); const QString activeCodecName = QWebSettings::globalSettings()->defaultTextEncoding();
QMenu* menuUTF = new QMenu("UTF", this);
QMenu* menuWindows = new QMenu("Windows", this);
QMenu* menuIscii = new QMenu("Iscii", this);
QMenu* menuOther = new QMenu(tr("Other"), this);
QList<QByteArray> available = QTextCodec::availableCodecs(); QStringList isoCodecs, utfCodecs, windowsCodecs, isciiCodecs, otherCodecs;
qSort(available);
const QString activeCodec = QWebSettings::globalSettings()->defaultTextEncoding();
foreach (const QByteArray &name, available) { foreach (const int mib, QTextCodec::availableMibs()) {
QTextCodec* codec = QTextCodec::codecForName(name); const QString codecName = QString::fromUtf8(QTextCodec::codecForMib(mib)->name());
if (codec && codec->aliases().contains(name)) {
continue;
}
const QString nameString = QString::fromUtf8(name); if (codecName.startsWith(QLatin1String("ISO")) && !isoCodecs.contains(codecName)) {
isoCodecs << codecName;
QAction* action = new QAction(nameString, 0);
action->setData(nameString);
action->setCheckable(true);
connect(action, SIGNAL(triggered()), this, SLOT(changeEncoding()));
if (activeCodec.compare(nameString, Qt::CaseInsensitive) == 0) {
action->setChecked(true);
} }
else if (codecName.startsWith(QLatin1String("UTF")) && !utfCodecs.contains(codecName)) {
if (nameString.startsWith(QLatin1String("ISO"))) { qDebug() << codecName << QTextCodec::codecForName(codecName.toUtf8())->mibEnum();
menuISO->addAction(action); utfCodecs << codecName;
} }
else if (nameString.startsWith(QLatin1String("UTF"))) { else if (codecName.startsWith(QLatin1String("windows"))
menuUTF->addAction(action); && !windowsCodecs.contains(codecName)) {
windowsCodecs << codecName;
} }
else if (nameString.startsWith(QLatin1String("windows"))) { else if (codecName.startsWith(QLatin1String("Iscii")) && !isciiCodecs.contains(codecName)) {
menuWindows->addAction(action); isciiCodecs << codecName;
} }
else if (nameString.startsWith(QLatin1String("Iscii"))) { else if (codecName == QLatin1String("System")) {
menuIscii->addAction(action); menu->addAction(createEncodingAction(codecName, activeCodecName, menu));
} }
else if (nameString == QLatin1String("System")) { else if (!otherCodecs.contains(codecName)) {
menu->addAction(action); otherCodecs << codecName;
}
else {
menuOther->addAction(action);
} }
} }
if (!menu->isEmpty()) {
menu->addSeparator(); menu->addSeparator();
if (!menuISO->isEmpty()) {
menu->addMenu(menuISO);
}
if (!menuUTF->isEmpty()) {
menu->addMenu(menuUTF);
}
if (!menuWindows->isEmpty()) {
menu->addMenu(menuWindows);
}
if (!menuIscii->isEmpty()) {
menu->addMenu(menuIscii);
}
if (!menuOther->isEmpty()) {
menu->addMenu(menuOther);
} }
createEncodingSubMenu("ISO", isoCodecs, menu);
createEncodingSubMenu("UTF", utfCodecs, menu);
createEncodingSubMenu("Windows", windowsCodecs, menu);
createEncodingSubMenu("Iscii", isciiCodecs, menu);
createEncodingSubMenu(tr("Other"), otherCodecs, menu);
} }
void BrowserWindow::addTab() void BrowserWindow::addTab()

View File

@ -175,6 +175,10 @@ private:
void setupUi(); void setupUi();
void setupMenu(); void setupMenu();
QAction *createEncodingAction(const QString &codecName, const QString &activeCodecName,
QMenu *menu);
void createEncodingSubMenu(const QString &name, QStringList &codecNames, QMenu *menu);
QUrl m_startUrl; QUrl m_startUrl;
QUrl m_homepage; QUrl m_homepage;
Qz::BrowserWindowType m_windowType; Qz::BrowserWindowType m_windowType;