kcm_add_layout_dialog.cpp 6.28 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 *  Copyright (C) 2010 Andriy Rysin (rysin@kde.org)
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include "kcm_add_layout_dialog.h"
20
#include <QDebug>
Laurent Montel's avatar
Laurent Montel committed
21
#include <KLocalizedString>
22 23 24 25

#include "xkb_rules.h"
#include "flags.h"
#include "iso_codes.h"
26
#include "tastenbrett.h"
27 28 29

#include "ui_kcm_add_layout_dialog.h"

30

31 32
AddLayoutDialog::AddLayoutDialog(const Rules* rules_, Flags* flags_, const QString& model_,
                                 const QStringList &options_, bool showLabel, QWidget* parent):
33 34 35
		QDialog(parent),
		rules(rules_),
		flags(flags_),
36 37
        model(model_),
        options(options_),
38
		selectedLanguage(QStringLiteral("no_language"))
39 40 41 42 43
{
    layoutDialogUi = new Ui_AddLayoutDialog();
    layoutDialogUi->setupUi(this);

    QSet<QString> languages;
44
    for (const LayoutInfo *layoutInfo  : rules->layoutInfos) {
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
45
    	QSet<QString> langs = QSet<QString>(layoutInfo->languages.constBegin(), layoutInfo->languages.constEnd());
46 47 48
    	languages.unite( langs );
    }
    IsoCodes isoCodes(IsoCodes::iso_639_3);
49
    for (const QString &lang : qAsConst(languages)) {
50 51 52 53 54
    	const IsoCodeEntry* isoCodeEntry = isoCodes.getEntry(IsoCodes::attr_iso_639_3_id, lang);
//    	const IsoCodeEntry* isoCodeEntry = isoCodes.getEntry(IsoCodes::attr_iso_639_2B_code, lang);
//    	if( isoCodeEntry == NULL ) {
//    		isoCodeEntry = isoCodes.getEntry(IsoCodes::attr_iso_639_2T_code, lang);
//    	}
55
    	QString name = isoCodeEntry != nullptr ? i18n(isoCodeEntry->value(IsoCodes::attr_name).toUtf8()) : lang;
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
    	layoutDialogUi->languageComboBox->addItem(name, lang);
    }
    layoutDialogUi->languageComboBox->model()->sort(0);
	layoutDialogUi->languageComboBox->insertItem(0, i18n("Any language"), "");
	layoutDialogUi->languageComboBox->setCurrentIndex(0);

	if( showLabel ) {
		layoutDialogUi->labelEdit->setMaxLength(LayoutUnit::MAX_LABEL_LENGTH);
	}
	else {
		layoutDialogUi->labelLabel->setVisible(false);
		layoutDialogUi->labelEdit->setVisible(false);
	}

    languageChanged(0);
71 72
    connect(layoutDialogUi->languageComboBox, static_cast<void (QComboBox::*)(int)>(&QComboBox::activated), this, &AddLayoutDialog::languageChanged);
    connect(layoutDialogUi->layoutComboBox, static_cast<void (QComboBox::*)(int)>(&QComboBox::activated), this, &AddLayoutDialog::layoutChanged);
73

74
    connect(layoutDialogUi->prevbutton, &QPushButton::clicked, this, &AddLayoutDialog::preview);
75
    layoutDialogUi->prevbutton->setVisible(Tastenbrett::exists());
76 77 78 79 80 81 82 83 84 85 86 87 88 89
}

void AddLayoutDialog::languageChanged(int langIdx)
{
	QString lang = layoutDialogUi->languageComboBox->itemData(langIdx).toString();
	if( lang == selectedLanguage )
		return;

	QPixmap emptyPixmap(layoutDialogUi->layoutComboBox->iconSize());
	emptyPixmap.fill(Qt::transparent);

	layoutDialogUi->layoutComboBox->clear();
	int defaultIndex = -1;
	int i = 0;
90
    for (const LayoutInfo *layoutInfo : qAsConst(rules->layoutInfos)) {
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
    	if( lang.isEmpty() || layoutInfo->isLanguageSupportedByLayout(lang) ) {
    		if( flags ) {
    			QIcon icon(flags->getIcon(layoutInfo->name));
    			if( icon.isNull() ) {
    				icon = QIcon(emptyPixmap);	// align text with no icons
    			}
    			layoutDialogUi->layoutComboBox->addItem(icon, layoutInfo->description, layoutInfo->name);
    		}
    		else {
    			layoutDialogUi->layoutComboBox->addItem(layoutInfo->description, layoutInfo->name);
    		}

    		// try to guess best default layout selection for given language
    		if( ! lang.isEmpty() && defaultIndex == -1 && layoutInfo->isLanguageSupportedByDefaultVariant(lang) ) {
    			defaultIndex = i;
    		}
    		i++;
    	}
    }
    if( defaultIndex == -1 ) {
    	defaultIndex = 0;
    }

    layoutDialogUi->layoutComboBox->model()->sort(0);
	layoutDialogUi->layoutComboBox->setCurrentIndex(defaultIndex);
	layoutChanged(defaultIndex);

	selectedLanguage = lang;
}

void AddLayoutDialog::layoutChanged(int layoutIdx)
{
	QString layoutName = layoutDialogUi->layoutComboBox->itemData(layoutIdx).toString();
	if( layoutName == selectedLayout )
		return;

	QString lang = layoutDialogUi->languageComboBox->itemData(layoutDialogUi->languageComboBox->currentIndex()).toString();

	layoutDialogUi->variantComboBox->clear();
	const LayoutInfo* layoutInfo = rules->getLayoutInfo(layoutName);
131
    for (const VariantInfo *variantInfo : layoutInfo->variantInfos) {
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
        if( lang.isEmpty() || layoutInfo->isLanguageSupportedByVariant(variantInfo, lang) ) {
        	layoutDialogUi->variantComboBox->addItem(variantInfo->description, variantInfo->name);
        }
    }

	layoutDialogUi->variantComboBox->model()->sort(0);

    if( lang.isEmpty() || layoutInfo->isLanguageSupportedByDefaultVariant(lang) ) {
    	layoutDialogUi->variantComboBox->insertItem(0, i18nc("variant", "Default"), "");
    }
	layoutDialogUi->variantComboBox->setCurrentIndex(0);

	layoutDialogUi->labelEdit->setText(layoutName);

	selectedLayout = layoutName;
}

void AddLayoutDialog::accept()
{
151 152
    selectedLayoutUnit.setLayout(layoutDialogUi->layoutComboBox->itemData(layoutDialogUi->layoutComboBox->currentIndex()).toString());
    selectedLayoutUnit.setVariant(layoutDialogUi->variantComboBox->itemData(layoutDialogUi->variantComboBox->currentIndex()).toString());
153
    QString label = layoutDialogUi->labelEdit->text();
154
    if( label == selectedLayoutUnit.layout() ) {
155
		label = QLatin1String("");
156 157 158 159 160 161
	}
	selectedLayoutUnit.setDisplayName( label );
	selectedLayoutUnit.setShortcut(layoutDialogUi->kkeysequencewidget->keySequence());
	QDialog::accept();
}

162 163
void AddLayoutDialog::preview()
{
164 165
    int index = layoutDialogUi->variantComboBox->currentIndex();
    QString variant = layoutDialogUi->variantComboBox->itemData(index).toString();
166
    Tastenbrett::launch(model, selectedLayout, variant, options.join(','));
167
}