示例#1
0
bool FskinCreator::create(const QString& path, int width, int height, QImage& img)
{
    if (!QFile::exists(path))
        return false;

    KTar tar(path);
    if (!tar.open(QIODevice::ReadOnly))
        return false;

    QStringList entries = tar.directory()->entries();
    if (entries.count() != 1)
        return false;

    const KArchiveEntry* entry = tar.directory()->entry(entries.first());
    if (!entry->isDirectory())
        return false;

    const KArchiveDirectory* subdir = static_cast<const KArchiveDirectory*>(entry);
    const KArchiveEntry* skinconf_entry = subdir->entry("fcitx_skin.conf");
    const KArchiveFile* skinconf = static_cast<const KArchiveFile*>(skinconf_entry);
    if (!skinconf)
        return false;

    QByteArray data = skinconf->data();

    /// parse ini file content
    bool skinfont = false;
    bool skininputbar = false;

    QPixmap skin;
    QString resizemode;
    /// margins
    int ml = 0, mr = 0, mt = 0, mb = 0;
    /// font size
    QFont font;
    QColor color_en;
    QColor color_label;
    QColor color_ch_1st;
    /// text y position
    int yen = 0, ych = 0;
    /// cursor color
    QColor color_cursor;
    /// back arrow
    QPixmap barrow;
    int xba = 0, yba = 0;
    /// forward arrow
    QPixmap farrow;
    int xfa = 0, yfa = 0;

    QTextStream ss(data);
    QString line;
    QString key, value;
    do {
        line = ss.readLine();
        if (line.isEmpty() || line.at(0) == '#')
            continue;

        if (line.at(0) == '[') {
            skinfont = (line == "[SkinFont]");
            skininputbar = (line == "[SkinInputBar]");
            continue;
        }

        if (!line.contains('='))
            continue;

        key = line.split('=').at(0);
        value = line.split('=').at(1);

        if (value.isEmpty())
            continue;

        if (skinfont) {
            if (key == "FontSize") {
                font.setPixelSize(value.toInt());
            }
            else if (key == "InputColor") {
                color_en = value2color(value);
            }
            else if (key == "IndexColor") {
                color_label = value2color(value);
            }
            else if (key == "FirstCandColor") {
                color_ch_1st = value2color(value);
            }
            else if (key == "UserPhraseColor") {
                QColor color_ch_user = value2color(value);
            }
            else if (key == "OtherColor") {
                QColor color_ch_other = value2color(value);
            }
        }
        else if (skininputbar) {
            if (key == "BackImg") {
                const KArchiveEntry* e = subdir->entry(value);
                const KArchiveFile* pix = static_cast<const KArchiveFile*>(e);
                if (pix)
                    skin.loadFromData(pix->data());
            }
            else if (key == "Resize") {
                resizemode = value;
            }
            else if (key == "MarginLeft") {
                ml = value.toInt();
            }
            else if (key == "MarginRight") {
                mr = value.toInt();
            }
            else if (key == "MarginTop") {
                mt = value.toInt();
            }
            else if (key == "MarginBottom") {
                mb = value.toInt();
            }
            else if (key == "InputPos") {
                yen = value.toInt();
            }
            else if (key == "OutputPos") {
                ych = value.toInt();
            }
            else if (key == "CursorColor") {
                color_cursor = value2color(value);
            }
            else if (key == "BackArrow") {
                const KArchiveEntry* e = subdir->entry(value);
                const KArchiveFile* pix = static_cast<const KArchiveFile*>(e);
                if (pix)
                    barrow.loadFromData(pix->data());
            }
            else if (key == "ForwardArrow") {
                const KArchiveEntry* e = subdir->entry(value);
                const KArchiveFile* pix = static_cast<const KArchiveFile*>(e);
                if (pix)
                    farrow.loadFromData(pix->data());
            }
            else if (key == "BackArrowX") {
                xba = value.toInt();
            }
            else if (key == "BackArrowY") {
                yba = value.toInt();
            }
            else if (key == "ForwardArrowX") {
                xfa = value.toInt();
            }
            else if (key == "ForwardArrowY") {
                yfa = value.toInt();
            }
        }
    }
    while (!line.isNull());

    int fontHeight = QFontMetrics(font).height();
    int pinyinw = QFontMetrics(font).width("ABC pinyin");
    int zhongwenw = QFontMetrics(font).width("1candidate");

    /// save target size
    int targetHeight = height;
    int targetWidth = width;

    height = mt + ych + mb;
    width = qMax(ml + pinyinw + mr, ml + zhongwenw + mr);
    width = qMax(width, targetWidth);
    width = qMax(width, skin.width());

    yen = mt + yen - fontHeight;
    ych = mt + ych - fontHeight;

    QPixmap pixmap(width, height);
    pixmap.fill(Qt::transparent);

    QPainter p(&pixmap);

    /// corners lt, lb, rt, rb
    p.drawPixmap(0, 0, skin,
                 0, 0, ml, mt);
    p.drawPixmap(0, height - mb, skin,
                 0, skin.height() - mb, ml, mb);
    p.drawPixmap(width - mr, 0, skin,
                 skin.width() - mr, 0, mr, mt);
    p.drawPixmap(width - mr, height - mb, skin,
                 skin.width() - mr, skin.height() - mb, mr, mb);

    /// left right
    p.drawPixmap(0, mt, ml, height - mt - mb, skin,
                 0, mt, ml, skin.height() - mt - mb);
    p.drawPixmap(width - mr, mt, mr, height - mt - mb, skin,
                 skin.width() - mr, mt, mr, skin.height() - mt - mb);

    /// top bottom
    p.drawPixmap(ml, 0, width - ml - mr, mt, skin,
                 ml, 0, skin.width() - ml - mr, mt);
    p.drawPixmap(ml, height - mb, width - ml - mr, mb, skin,
                 ml, skin.height() - mb, skin.width() - ml - mr, mb);

    /// middle
    p.drawPixmap(ml, mt, width - ml - mr, height - mt - mb, skin,
                 ml, mt, skin.width() - ml - mr, skin.height() - mt - mb);

    /// draw arrows
    p.drawPixmap(width - xba, yba, barrow);
    p.drawPixmap(width - xfa, yfa, farrow);

    /// draw preedit / aux text
    p.setFont(font);
    p.setPen(color_en);
    p.drawText(ml, yen, pinyinw, fontHeight, Qt::AlignCenter, "ABC pinyin");
    p.drawLine(ml + pinyinw, yen, ml + pinyinw, yen + fontHeight);

    /// draw lookup table
    p.setPen(color_label);
    int labelw = p.fontMetrics().width("1");
    p.drawText(ml, ych, labelw, fontHeight, Qt::AlignCenter, "1");
    p.setPen(color_ch_1st);
    int candidatew = p.fontMetrics().width("candidate");
    p.drawText(ml + labelw, ych, candidatew, fontHeight, Qt::AlignCenter, "candidate");

    p.end();

    if (targetWidth < width || targetHeight < height) {
        pixmap = pixmap.scaled(targetWidth, targetHeight, Qt::KeepAspectRatio);
    }

    img = pixmap.toImage();

    return true;
}
void EditingSkinDialog::loadMainConf()
{
    mSettings->beginGroup("SkinInfo");
    QString skinAuthor = mSettings->value("Author").toString();
    QString skinVersion = mSettings->value("Version").toString();

    ui->lineEditSkinAuthor->setText(skinAuthor);
    ui->lineEditSkinVersion->setText(skinVersion);

    mSettings->endGroup();

    mSettings->beginGroup("SkinFont");
    int fontSize = mSettings->value("FontSize").toInt();
    int candFontSize = mSettings->value("CandFontSize").toInt();
    QString inputColor = mSettings->value("InputColor").toString();
    QString indexColor = mSettings->value("IndexColor").toString();
    QString firstCandColor = mSettings->value("FirstCandColor").toString();
    QString otherColor = mSettings->value("OtherColor").toString();
    inputColorConf = inputColor;
    indexColorConf = indexColor;
    firstCandColorConf = firstCandColor;
    otherColorConf = otherColor;
    ui->spinBoxInputFontSize->setValue(fontSize);
    ui->spinBoxCandFontSize->setValue(candFontSize);

    ui->pushButtonInputColor->setStyleSheet("QPushButton { background-color:  " + colorToRGB(value2color(inputColor)) +";border: none;" +"}");
    ui->pushButtonIndexColor->setStyleSheet("QPushButton { background-color:  " + colorToRGB(value2color(indexColor)) +";border: none;" +"}");
    ui->pushButtonFirstCandColor->setStyleSheet("QPushButton { background-color:  " + colorToRGB(value2color(firstCandColor)) +";border: none;" +"}");
    ui->pushButtonOtherCandColor->setStyleSheet("QPushButton { background-color:  " + colorToRGB(value2color(otherColor)) +";border: none;" +"}");
    mSettings->endGroup();

    mSettings->beginGroup("SkinInputBar");
    QString backImg = mSettings->value("BackImg").toString();
    QString tipsImg = mSettings->value("TipsImg").toString();
    int marginLeft = mSettings->value("MarginLeft").toInt();
    int marginRight = mSettings->value("MarginRight").toInt();
    int marginTop = mSettings->value("MarginTop").toInt();
    int marginBottom = mSettings->value("MarginBottom").toInt();

    int inputStringPosX = mSettings->value("InputStringPosX").toInt();
    int inputStringPosY = mSettings->value("InputStringPosY").toInt();
    int outputCandPosX = mSettings->value("OutputCandPosX").toInt();
    int outputCandPosY = mSettings->value("OutputCandPosY").toInt();
    QString backArrow = mSettings->value("BackArrow").toString();
    int backArrowX = mSettings->value("BackArrowX").toInt();
    int backArrowY = mSettings->value("BackArrowY").toInt();
    QString forwardArrow = mSettings->value("ForwardArrow").toString();
    int forwardArrowX = mSettings->value("ForwardArrowX").toInt();
    int forwardArrowY = mSettings->value("ForwardArrowY").toInt();
    int adjustWidth = mSettings->value("AdjustWidth").toInt();
    int adjustHeight = mSettings->value("AdjustHeight").toInt();
    mSettings->endGroup();

    mSettings->beginGroup("SkinInputBarVertical");
    int adjustWidth_v = mSettings->value("AdjustWidth").toInt();
    int adjustHeight_v = mSettings->value("AdjustHeight").toInt();
    QString backImg_v = mSettings->value("BackImg").toString();
    int marginTop_v = mSettings->value("MarginTop").toInt();
    int marginBottom_v = mSettings->value("MarginBottom").toInt();
    mSettings->endGroup();

    ui->lineEdit_iTipsImg->setText(tipsImg);
    ui->spinBox_iLeftMargin->setValue(marginLeft);
    ui->spinBox_iRightMargin->setValue(marginRight);

    ui->spinBox_iInputStringPosX->setValue(inputStringPosX);
    ui->spinBox_iInputStringPosY->setValue(inputStringPosY);
    ui->spinBox_iOutputCandPosX->setValue(outputCandPosX);
    ui->spinBox_iOutputCandPosY->setValue(outputCandPosY);
    ui->lineEdit_iBackArrow->setText(backArrow);
    ui->spinBox_iBackArrowX->setValue(backArrowX);
    ui->spinBox_iBackArrowY->setValue(backArrowY);
    ui->lineEdit_iForwardArrow->setText(forwardArrow);
    ui->spinBox_iForwardArrowX->setValue(forwardArrowX);
    ui->spinBox_iForwardArrowY->setValue(forwardArrowY);
    if(mHorizontal == true)
    {
        ui->spinBox_iAdjustHeight->setValue(adjustHeight);
        ui->spinBox_iAdjustWidth->setValue(adjustWidth);
        ui->lineEdit_iBackImg->setText(backImg);
        ui->spinBox_iTopMargin->setValue(marginTop);
        ui->spinBox_iBottomMargin->setValue(marginBottom);
    }
    else
    {
        ui->spinBox_iAdjustHeight->setValue(adjustHeight_v);
        ui->spinBox_iAdjustWidth->setValue(adjustWidth_v);
        ui->lineEdit_iBackImg->setText(backImg_v);
        ui->spinBox_iTopMargin->setValue(marginTop_v);
        ui->spinBox_iBottomMargin->setValue(marginBottom_v);
    }
}