⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 qcolordialog.cpp

📁 奇趣公司比较新的qt/emd版本
💻 CPP
📖 第 1 页 / 共 3 页
字号:
    if (alphaEd->isVisible())        curCol = qRgba(rEd->value(), gEd->value(), bEd->value(), currentAlpha());    else        curCol = qRgb(rEd->value(), gEd->value(), bEd->value());    rgb2hsv(currentColor(), hue, sat, val);    hEd->setValue(hue);    sEd->setValue(sat);    vEd->setValue(val);    showCurrentColor();    emit newCol(currentColor());}void QColorShower::hsvEd(){    rgbOriginal = false;    hue = hEd->value();    sat = sEd->value();    val = vEd->value();    QColor c;    c.setHsv(hue, sat, val);    curCol = c.rgb();    rEd->setValue(qRed(currentColor()));    gEd->setValue(qGreen(currentColor()));    bEd->setValue(qBlue(currentColor()));    showCurrentColor();    emit newCol(currentColor());}void QColorShower::setRgb(QRgb rgb){    rgbOriginal = true;    curCol = rgb;    rgb2hsv(currentColor(), hue, sat, val);    hEd->setValue(hue);    sEd->setValue(sat);    vEd->setValue(val);    rEd->setValue(qRed(currentColor()));    gEd->setValue(qGreen(currentColor()));    bEd->setValue(qBlue(currentColor()));    showCurrentColor();}void QColorShower::setHsv(int h, int s, int v){    if (h < -1 || (uint)s > 255 || (uint)v > 255)	return;    rgbOriginal = false;    hue = h; val = v; sat = s;    QColor c;    c.setHsv(hue, sat, val);    curCol = c.rgb();    hEd->setValue(hue);    sEd->setValue(sat);    vEd->setValue(val);    rEd->setValue(qRed(currentColor()));    gEd->setValue(qGreen(currentColor()));    bEd->setValue(qBlue(currentColor()));    showCurrentColor();}void QColorShower::retranslateStrings(){    lblHue->setText(QColorDialog::tr("Hu&e:"));    lblSat->setText(QColorDialog::tr("&Sat:"));    lblVal->setText(QColorDialog::tr("&Val:"));    lblRed->setText(QColorDialog::tr("&Red:"));    lblGreen->setText(QColorDialog::tr("&Green:"));    lblBlue->setText(QColorDialog::tr("Bl&ue:"));    alphaLab->setText(QColorDialog::tr("A&lpha channel:"));}class QColorDialogPrivate : public QDialogPrivate{    Q_DECLARE_PUBLIC(QColorDialog)public:    void init();    QRgb currentColor() const { return cs->currentColor(); }    void setCurrentColor(QRgb rgb);    int currentAlpha() const { return cs->currentAlpha(); }    void setCurrentAlpha(int a) { cs->setCurrentAlpha(a); }    void showAlpha(bool b) { cs->showAlpha(b); }    void retranslateStrings();    void _q_addCustom();    void _q_newHsv(int h, int s, int v);    void _q_newColorTypedIn(QRgb rgb);    void _q_newCustom(int, int);    void _q_newStandard(int, int);    QWellArray *custom;    QWellArray *standard;    QColorPicker *cp;    QColorLuminancePicker *lp;    QColorShower *cs;    QLabel *lblBasicColors;    QLabel *lblCustomColors;    QPushButton *custbut;    QPushButton *ok;    QPushButton *cancel;    QPushButton *addCusBt;    int nextCust;    bool compact;};//sets all widgets to display h,s,vvoid QColorDialogPrivate::_q_newHsv(int h, int s, int v){    cs->setHsv(h, s, v);    cp->setCol(h, s);    lp->setCol(h, s, v);}//sets all widgets to display rgbvoid QColorDialogPrivate::setCurrentColor(QRgb rgb){    cs->setRgb(rgb);    _q_newColorTypedIn(rgb);}//sets all widgets except cs to display rgbvoid QColorDialogPrivate::_q_newColorTypedIn(QRgb rgb){    int h, s, v;    rgb2hsv(rgb, h, s, v);    cp->setCol(h, s);    lp->setCol(h, s, v);}void QColorDialogPrivate::_q_newCustom(int r, int c){    int i = r+2*c;    setCurrentColor(cusrgb[i]);    nextCust = i;    if (standard)        standard->setSelected(-1,-1);}void QColorDialogPrivate::_q_newStandard(int r, int c){    setCurrentColor(stdrgb[r+c*6]);    if (custom)        custom->setSelected(-1,-1);}void QColorDialogPrivate::init(){    Q_Q(QColorDialog);    compact = false;    // small displays (e.g. PDAs cannot fit the full color dialog,    // so just use the color picker.    if (qApp->desktop()->width() < 480 || qApp->desktop()->height() < 350)        compact = true;    nextCust = 0;    QHBoxLayout *topLay = new QHBoxLayout(q);    const int lumSpace = topLay->spacing() / 2;    QVBoxLayout *leftLay = 0;    if (!compact) {        leftLay = new QVBoxLayout;        topLay->addLayout(leftLay);    }    initRGB();    if (!compact) {        standard = new QColorWell(q, 6, 8, stdrgb);        lblBasicColors = new QLabel(q);#ifndef QT_NO_SHORTCUT        lblBasicColors->setBuddy(standard);#endif        q->connect(standard, SIGNAL(selected(int,int)), SLOT(_q_newStandard(int,int)));        leftLay->addWidget(lblBasicColors);        leftLay->addWidget(standard);        leftLay->addStretch();        custom = new QColorWell(q, 2, 8, cusrgb);        custom->setAcceptDrops(true);        q->connect(custom, SIGNAL(selected(int,int)), SLOT(_q_newCustom(int,int)));        lblCustomColors = new QLabel(q);#ifndef QT_NO_SHORTCUT        lblCustomColors->setBuddy(custom);#endif        leftLay->addWidget(lblCustomColors);        leftLay->addWidget(custom);        custbut = new QPushButton(q);        custbut->setEnabled(false);        leftLay->addWidget(custbut);    } else {        // better color picker size for small displays        pWidth = 150;        pHeight = 100;        custom = 0;        standard = 0;    }    QVBoxLayout *rightLay = new QVBoxLayout;    topLay->addLayout(rightLay);    QHBoxLayout *pickLay = new QHBoxLayout;    rightLay->addLayout(pickLay);    QVBoxLayout *cLay = new QVBoxLayout;    pickLay->addLayout(cLay);    cp = new QColorPicker(q);    cp->setFrameStyle(QFrame::Panel + QFrame::Sunken);    cLay->addSpacing(lumSpace);    cLay->addWidget(cp);    cLay->addSpacing(lumSpace);    lp = new QColorLuminancePicker(q);    lp->setFixedWidth(20);    pickLay->addWidget(lp);    QObject::connect(cp, SIGNAL(newCol(int,int)), lp, SLOT(setCol(int,int)));    QObject::connect(lp, SIGNAL(newHsv(int,int,int)), q, SLOT(_q_newHsv(int,int,int)));    rightLay->addStretch();    cs = new QColorShower(q);    QObject::connect(cs, SIGNAL(newCol(QRgb)), q, SLOT(_q_newColorTypedIn(QRgb)));    rightLay->addWidget(cs);    QHBoxLayout *buttons;    if (compact) {        buttons = new QHBoxLayout;        rightLay->addLayout(buttons);    } else {        buttons = new QHBoxLayout;        leftLay->addLayout(buttons);    }    ok = new QPushButton(q);    QObject::connect(ok, SIGNAL(clicked()), q, SLOT(accept()));    ok->setDefault(true);    cancel = new QPushButton(q);    QObject::connect(cancel, SIGNAL(clicked()), q, SLOT(reject()));    buttons->addWidget(ok);    buttons->addWidget(cancel);    buttons->addStretch();    if (!compact) {        addCusBt = new QPushButton(q);        rightLay->addWidget(addCusBt);        QObject::connect(addCusBt, SIGNAL(clicked()), q, SLOT(_q_addCustom()));    }    retranslateStrings();}void QColorDialogPrivate::_q_addCustom(){    cusrgb[nextCust] = cs->currentColor();    if (custom)        custom->update();    nextCust = (nextCust+1) % 16;}void QColorDialogPrivate::retranslateStrings(){    if (!compact) {        lblBasicColors->setText(QColorDialog::tr("&Basic colors"));        lblCustomColors->setText(QColorDialog::tr("&Custom colors"));        custbut->setText(QColorDialog::tr("&Define Custom Colors >>"));        addCusBt->setText(QColorDialog::tr("&Add to Custom Colors"));    }    ok->setText(QColorDialog::tr("OK"));    cancel->setText(QColorDialog::tr("Cancel"));    cs->retranslateStrings();}/*!    \class QColorDialog    \brief The QColorDialog class provides a dialog widget for specifying colors.    \mainclass    \ingroup dialogs    \ingroup multimedia    The color dialog's function is to allow users to choose colors.    For example, you might use this in a drawing program to allow the    user to set the brush color.    The static functions provide modal color dialogs.    \omit    If you require a modeless dialog, use the QColorDialog constructor.    \endomit    The static getColor() function shows the dialog, and allows the    user to specify a color. The getRgba() function does the same, but    also allows the user to specify a color with an alpha channel    (transparency) value.    The user can store customCount() different custom colors. The    custom colors are shared by all color dialogs, and remembered    during the execution of the program. Use setCustomColor() to set    the custom colors, and use customColor() to get them.    Additional widgets that allow users to pick colors are available    as \l{Qt Solutions}.    The \l{dialogs/standarddialogs}{Standard Dialogs} example shows    how to use QColorDialog as well as other built-in Qt dialogs.    \image plastique-colordialog.png A color dialog in the Plastique widget style.    \sa QColor, QFileDialog, QPrintDialog, QFontDialog, {Standard Dialogs Example}*//*!    Constructs a default color dialog called \a name with the given \a parent.    If \a modal is true the dialog will be modal. Use setColor() to set an    initial value.    \sa getColor()*/QColorDialog::QColorDialog(QWidget* parent, bool modal) :    QDialog(*new QColorDialogPrivate, parent, (Qt::Dialog | Qt::WindowTitleHint |                     Qt::MSWindowsFixedSizeDialogHint | Qt::WindowSystemMenuHint)){    Q_D(QColorDialog);    setModal(modal);    setSizeGripEnabled(false);    d->init();#ifndef QT_NO_SETTINGS    if (!customSet) {        QSettings settings(QSettings::UserScope, QLatin1String("Trolltech"));        settings.beginGroup(QLatin1String("Qt"));        for (int i = 0; i < 2*8; ++i) {            QVariant v = settings.value(QLatin1String("customColors/") + QString::number(i));            if (v.isValid()) {                QRgb rgb = v.toUInt();                cusrgb[i] = rgb;            }        }        settings.endGroup(); // Qt    }#endif}/*!    Pops up a modal color dialog, lets the user choose a color, and    returns that color. The color is initially set to \a initial. The    dialog is a child of \a parent. It returns an invalid (see    QColor::isValid()) color if the user cancels the dialog. All    colors allocated by the dialog will be deallocated before this    function returns.*/QColor QColorDialog::getColor(const QColor& initial, QWidget *parent){#if defined(Q_WS_MAC)    return macGetColor(initial, parent);#endif    QColorDialog *dlg = new QColorDialog(parent, true);  //modal    dlg->setWindowTitle(QColorDialog::tr("Select color"));    dlg->setColor(initial);    dlg->selectColor(initial);    int resultCode = dlg->exec();    QColor result;    if (resultCode == QDialog::Accepted)        result = dlg->color();    delete dlg;    return result;}/*!    Pops up a modal color dialog to allow the user to choose a color    and an alpha channel (transparency) value. The color+alpha is    initially set to \a initial. The dialog is a child of \a parent.    If \a ok is non-null, \e *\a ok is set to true if the user clicked    OK, and to false if the user clicked Cancel.    If the user clicks Cancel, the \a initial value is returned.*/QRgb QColorDialog::getRgba(QRgb initial, bool *ok, QWidget *parent){#if defined(Q_WS_MAC)    return macGetRgba(initial, true, ok, parent);#endif    QColorDialog *dlg = new QColorDialog(parent, true);  //modal    dlg->setWindowTitle(QColorDialog::tr("Select color"));    dlg->setColor(initial);    dlg->selectColor(initial);    dlg->setSelectedAlpha(qAlpha(initial));    int resultCode = dlg->exec();    QRgb result = initial;    if (resultCode == QDialog::Accepted) {        QRgb c = dlg->color().rgb();        int alpha = dlg->selectedAlpha();        result = qRgba(qRed(c), qGreen(c), qBlue(c), alpha);    }    if (ok)        *ok = resultCode == QDialog::Accepted;    delete dlg;    return result;}/*!    Returns the currently selected color in the dialog.    \sa setColor()*/QColor QColorDialog::color() const{    Q_D(const QColorDialog);    return QColor(d->currentColor());}/*!    Destroys the color dialog.*/QColorDialog::~QColorDialog(){#ifndef QT_NO_SETTINGS    if (!customSet) {        QSettings settings(QSettings::UserScope, QLatin1String("Trolltech"));        settings.beginGroup(QLatin1String("Qt"));        for (int i = 0; i < 2*8; ++i)            settings.setValue(QLatin1String("customColors/") + QString::number(i), cusrgb[i]);        settings.endGroup();    }#endif}/*!    \reimp*/void QColorDialog::changeEvent(QEvent *e){    Q_D(QColorDialog);    if (e->type() == QEvent::LanguageChange) {        d->retranslateStrings();    }    QDialog::changeEvent(e);}/*!    \fn void QColorDialog::setColor(const QColor &color)    Sets the color shown in the dialog to the \a color given.    \sa color()*/void QColorDialog::setColor(const QColor& c){    Q_D(QColorDialog);    d->setCurrentColor(c.rgb());}/*!    \fn void QColorDialog::setSelectedAlpha(int alpha)    Sets the initial alpha channel value to the \a alpha value given, and    shows the alpha channel entry box.*/void QColorDialog::setSelectedAlpha(int a){    Q_D(QColorDialog);    d->showAlpha(true);    d->setCurrentAlpha(a);}/*!    Returns the value selected for the alpha channel.*/int QColorDialog::selectedAlpha() const{    Q_D(const QColorDialog);    return d->currentAlpha();}/*!    Sets focus to the corresponding button, if any.*/bool QColorDialog::selectColor(const QColor& col){    Q_D(QColorDialog);    QRgb color = col.rgb();    int i = 0, j = 0;    // Check standard colors    if (d->standard) {        for (i = 0; i < 6; i++) {            for (j = 0; j < 8; j++) {                if (color == stdrgb[i + j*6]) {                    d->_q_newStandard(i, j);                    d->standard->setCurrent(i, j);                    d->standard->setSelected(i, j);                    d->standard->setFocus();                    return true;                }            }        }    }    // Check custom colors    if (d->custom) {        for (i = 0; i < 2; i++) {            for (j = 0; j < 8; j++) {                if (color == cusrgb[i + j*2]) {                    d->_q_newCustom(i, j);                    d->custom->setCurrent(i, j);                    d->custom->setSelected(i, j);                    d->custom->setFocus();                    return true;                }            }        }    }    return false;}#include "qcolordialog.moc"#include "moc_qcolordialog.cpp"#endif/*!    \fn QColor QColorDialog::getColor(const QColor &init, QWidget *parent, const char *name)    \compat*//*!    \fn QRgb QColorDialog::getRgba(QRgb rgba, bool *ok, QWidget *parent, const char *name)    \compat*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -