pubcombolinecouplewideget.cpp 7.81 KB
Newer Older
‘oliver.hui’'s avatar
‘oliver.hui’ committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 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 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
#include "pubcombolinecouplewideget.h"
#include <QDebug>
#include <QList>
#include <tbaseutil/tenumlist.h>
#include <topcore/topenummanager.h>
#include <topcore/topclassabs.h>
#include <tbaseutil/tdataparse.h>
#include <twidget/thboxlayout.h>
#include <twidget/tpushbutton.h>
#include <tbaseutil/tresource.h>
#include <twidget/tlineedit.h>
#include <twidget/ttextedit.h>
#include <twidget/tlabel.h>
#include <twidget/teditablecombobox.h>
#include <twidget/ttitleexpander.h>
#include <twidget/tvboxlayout.h>
#include <twidget/tformlayout.h>
#include <twidget/tcombobox.h>

PubComboLineCoupleWideget::PubComboLineCoupleWideget(TopClassAbs *parent)
    : QWidget(parent), mParent(parent)
{
    initMainView();
    mNewItemButton = new TPushButton();
    mNewItemButton->setText(ttr("New Item"));
    mFramelayout->insertWidget(mItemWgtLst.length(), mNewItemButton, 1, Qt::AlignLeft);
    connect(mNewItemButton, SIGNAL(clicked(bool)), this, SLOT(newCoupleItem()));
    connect(mNewItemButton, SIGNAL(clicked(bool)), this, SIGNAL(dataChanged()));
}

QVariantMap PubComboLineCoupleWideget::getData()
{
    QVariantMap resMap;
    QStringList keyLst;
    QStringList itemValueLst;
    for (PubComboLineCoupleWidegetPrivate *wgt: mItemWgtLst) {
        if (!keyLst.contains(wgt->categoryText())) {
            keyLst.append(wgt->categoryText());
        }
    }

    for (QString keyStr: keyLst) {
        itemValueLst.clear();
        for (PubComboLineCoupleWidegetPrivate* wgt: mItemWgtLst) {
            if (keyStr == wgt->categoryText()) {
                itemValueLst.append(wgt->valueText());
            }
        }
        resMap.insert(keyStr,itemValueLst);
    }
    return resMap;
}

void PubComboLineCoupleWideget::setData(QVariant iData)
{
    QVariantMap valueMap;
    QVariantList resValueLst;

    if (iData.type() == QVariant::String) {
        valueMap = TDataParse::jsonStr2Variant(iData.toString()).toMap();
    } else if (iData.type() == QVariant::Map) {
        valueMap = iData.toMap();
    } else {
        valueMap = QVariantMap();
    }

    QStringList keyLst = valueMap.keys();
    for (QString keyStr: keyLst) {
        QStringList itemValueLst;
        itemValueLst = valueMap.value(keyStr).toStringList();
        QVariantMap resValueMap;
        for (QString oneValue: itemValueLst) {
            resValueMap.insert("name", enumText2Name(keyStr));
            resValueMap.insert("value", oneValue);
            resValueLst.append(resValueMap);
        }
    }

    //清除所有行
    for (PubComboLineCoupleWidegetPrivate *wgt: mItemWgtLst) {
        mItemWgtLst.removeOne(wgt);
        wgt->hide();//deleter来的较慢,避免视觉上重复出现
        wgt->deleteLater();
    }
    //按list的长度初始化控件(行)个数
    for (int count = 0; count < resValueLst.length(); count++) {
       newCoupleItem();
    }
    //对所有行逐一赋值
    for (PubComboLineCoupleWidegetPrivate *wgt: mItemWgtLst) {
        int index = mItemWgtLst.indexOf(wgt);
        QVariantMap itemMap = resValueLst.value(index).toMap();
        wgt->setName(itemMap.value("name").toString());
        wgt->setValue(itemMap.value("value").toString());
    }
}

void PubComboLineCoupleWideget::setEnumName(QString iEnumName)
{
    mEnumName = iEnumName;
}

void PubComboLineCoupleWideget::setComboBoxEditable(bool iIsEditable)
{
    mIsComboEditable = iIsEditable;
}

QString PubComboLineCoupleWideget::ttr(const QString &iStr)
{
    if (mParent) {
        return mParent->ttr(iStr);
    } else {
        return tr(iStr.toStdString().data());
    }
}

QString PubComboLineCoupleWideget::enumText2Name(QString iNameStr)
{
    if (TOPENM->isEnumListExists(mEnumName)){
        QVariantList lst = TOPENM->enumList(mEnumName)->toComboList();
        for (QVariant item: lst) {
            if (iNameStr == item.toMap().value("name")) {
                return item.toMap().value("text").toString();
            }
        }
    }
    return iNameStr;
}

void PubComboLineCoupleWideget::newCoupleItem()
{
    PubComboLineCoupleWidegetPrivate* wgt = new PubComboLineCoupleWidegetPrivate(this);
    wgt->setComboBoxEditable(mIsComboEditable);
    mItemWgtLst.append(wgt);
    mFramelayout->insertWidget(mItemWgtLst.length() - 1 , wgt);
    wgt->setCategoryEnum(mEnumName);
    connect(wgt, SIGNAL(dataChanged()), this, SIGNAL(dataChanged()));
    connect(wgt, SIGNAL(deleteClicked()), this, SLOT(onDeleteClicked()));
}

void PubComboLineCoupleWideget::initMainView()
{
    QHBoxLayout* hMainLayout = new QHBoxLayout(this);
    hMainLayout->setMargin(0);
    hMainLayout->setSpacing(0);
    this->setLayout(hMainLayout);
    mFrame = new QFrame(this);
    hMainLayout->addWidget(mFrame);
    mFramelayout = new TVBoxLayout(mFrame);
    mFramelayout->setMargin(8);
    mFramelayout->setSpacing(8);
}

void PubComboLineCoupleWideget::onDeleteClicked()
{
    PubComboLineCoupleWidegetPrivate* wgt = qobject_cast<PubComboLineCoupleWidegetPrivate *>(sender());
    mItemWgtLst.removeOne(wgt);
    mFramelayout->removeWidget(wgt);
    wgt->deleteLater();
    emit(dataChanged());
}

PubComboLineCoupleWidegetPrivate::PubComboLineCoupleWidegetPrivate(QWidget *parent)
{
    Q_UNUSED(parent);

    THBoxLayout* layout = new THBoxLayout(this);
    layout->setMargin(0);
    layout->setSpacing(0);
    mCategoryComboBox = new TEditableComboBox(this);
    mCategoryComboBox->setFixedWidth(100);
    mCategoryComboBox->setStyleSheet("QComboBox{border-top:0px;border-bottom:0px;border-left:0px;border-right:4px;border-right-style: solid;border-right-color:#F3F7FB;}");
    mValueLineEdit = new TLineEdit(this);
    mValueLineEdit->prependWidget(mCategoryComboBox);

    mDeleteButton = new TPushButton(this);
    mDeleteButton->setIcon(TRES->icon("minus-circle"));
    mDeleteButton->setFlat(true);
    mValueLineEdit->appendWidget(mDeleteButton);
    layout->addWidget(mValueLineEdit);
    connect(mCategoryComboBox, SIGNAL(currentTextChanged(QString)), this, SIGNAL(dataChanged()));
    connect(mValueLineEdit, SIGNAL(textChanged(QString)), this, SIGNAL(dataChanged()));
    connect(mDeleteButton, SIGNAL(clicked()), this, SIGNAL(deleteClicked()));
}

void PubComboLineCoupleWidegetPrivate::setCategoryEnum(QString iEnumName)
{
    if (TOPENM->isEnumListExists(iEnumName)) {
        mEnumName = iEnumName;
        QVariantList lst = TOPENM->enumList(iEnumName)->toComboList();
        QStringList strLst;
        for (QVariant item: lst) {
            strLst.append(item.toMap().value("text").toString());
        }
        mCategoryComboBox->setItemList(strLst);
    }
}

void PubComboLineCoupleWidegetPrivate::setComboBoxEditable(bool iIsEditable)
{
    mCategoryComboBox->setEditable(iIsEditable);
}

QString PubComboLineCoupleWidegetPrivate::categoryText()
{
    if (mCategoryComboBox) {
        //查找当前选中项对应的枚举
        if (TOPENM->isEnumListExists(mEnumName)) {
            QVariantList lst = TOPENM->enumList(mEnumName)->toComboList();
            for (QVariant item: lst) {
                if (mCategoryComboBox->currentText() == item.toMap().value("text")) {
                    return item.toMap().value("name").toString();
                }
            }
        }
    }
    return mCategoryComboBox->currentText();
}

QString PubComboLineCoupleWidegetPrivate::valueText()
{
    if (mValueLineEdit) {
        return mValueLineEdit->text();
    }
    return "";
}

void PubComboLineCoupleWidegetPrivate::clearData()
{
    mCategoryComboBox->clear();
    mValueLineEdit->clear();
}

void PubComboLineCoupleWidegetPrivate::setName(const QString &iNameStr)
{
    if (iNameStr.isEmpty() || iNameStr.isNull()) {
        mCategoryComboBox->setCurrentText("");
        return ;
    }
    mCategoryComboBox->setCurrentText(iNameStr);
}

void PubComboLineCoupleWidegetPrivate::setValue(const QString &iValueStr)
{
    if (iValueStr.isEmpty()|| iValueStr.isNull()) {
        mValueLineEdit->setText("");
        return ;
    }
    mValueLineEdit->setText(iValueStr);
}