qwwcolorcombobox.cpp

00001 //
00002 // C++ Implementation: %{MODULE}
00003 //
00004 // Description:
00005 //
00006 //
00007 // Author: %{AUTHOR} <%{EMAIL}>, (C) %{YEAR}
00008 //
00009 // Copyright: See COPYING file that comes with this distribution
00010 //
00011 //
00012 
00013 #ifndef WW_NO_COLORCOMBOBOX
00014 
00015 #include "qwwcolorcombobox.h"
00016 #include <QItemDelegate>
00017 #include <QColorDialog>
00018 #include <QAbstractItemView>
00019 #include <QMessageBox>
00020 #include <QLayout>
00021 #include <QPushButton>
00022 #include <QEvent>
00023 
00031 QwwColorComboBox::QwwColorComboBox(QWidget *parent)
00032         : QComboBox(parent) {
00033     connect(this, SIGNAL(highlighted(int)), this, SLOT(_q_highlighted(int)));
00034     connect(this, SIGNAL(activated(int)), this, SLOT(w_q_activated(int)));
00035     connect(this, SIGNAL(currentIndexChanged(int)), this, SLOT(_q_currentIndexChanged(int)));
00036     view()->installEventFilter(this);
00037     connect(view(), SIGNAL(clicked(const QModelIndex &)), SLOT(_q_popup(const QModelIndex&)));
00038     m_dlgEnabled = false;
00039 //       addColor(Qt::red, "xx");
00040 
00041 }
00042 
00043 
00044 QwwColorComboBox::~QwwColorComboBox() {}
00045 
00046 void QwwColorComboBox::addColor(const QColor & color, const QString & name) {
00047     insertColor(colorCount(), color, name);
00048 }
00049 
00050 void QwwColorComboBox::setCurrentColor(const QColor & color) {
00051     int i = findData(color);
00052     if (i!=-1) {
00053         setCurrentIndex(i);
00054     }
00055 }
00056 
00057 
00058 void QwwColorComboBox::insertColor(int index, const QColor & color, const QString & name) {
00059     QPixmap px(12, 12);
00060     px.fill(color);
00061     insertItem(index, QIcon(px), name, color);
00062 }
00063 
00064 QColor QwwColorComboBox::currentColor() const {
00065     return QColor();
00066 }
00067 
00068 int QwwColorComboBox::colorCount() const {
00069     return /*m_dlgEnabled ? count()-1 :*/ count();
00070 }
00071 
00072 QColor QwwColorComboBox::color(int index) const {
00073     return QColor();
00074 }
00075 
00076 void QwwColorComboBox::setStandardColors() {}
00077 
00078 
00079 bool QwwColorComboBox::isColorDialogEnabled() const {
00080     return m_dlgEnabled;
00081 }
00082 
00083 void QwwColorComboBox::setColorDialogEnabled(bool enabled) {
00084     if (m_dlgEnabled == enabled) return;
00085     m_dlgEnabled = enabled;
00086 //     if(enabled){
00087 //         addItem("More...");
00088 //     } else {
00089 //         removeItem(count()-1);
00090 //     }
00091 }
00092 
00093 
00094 
00095 void QwwColorComboBox::_q_currentIndexChanged(int i) {
00096     QVariant v = itemData(i, Qt::UserRole);
00097     if (v.isValid()) {
00098         QColor c = qvariant_cast<QColor>(v);
00099         if (c.isValid()) {
00100             emit currentIndexChanged(c);
00101         }
00102     }
00103 }
00104 
00105 void QwwColorComboBox::w_q_activated(int i) {
00106 //     QMessageBox::information(this, "", "ACTIVATED");
00107 
00108 //     if (m_dlgEnabled && i==count()-1) {
00109 //         popupDialog();
00110 //         return;
00111 //     }
00112     QVariant v = itemData(i, Qt::UserRole);
00113     if (v.isValid()) {
00114         QColor c = qvariant_cast<QColor>(v);
00115         if (c.isValid())
00116             emit activated(c);
00117 
00118     }
00119 }
00120 
00121 void QwwColorComboBox::_q_highlighted(int i) {
00122 //     QMessageBox::information(this, "BLAD", "HIGHLIGHTED");
00123     qDebug("HIGHLIGHTED: %d", i);
00124     QVariant v = itemData(i, Qt::UserRole);
00125     if (v.isValid()) {
00126         QColor c = qvariant_cast<QColor>(v);
00127         if (c.isValid())
00128             emit highlighted(c);
00129     }
00130 }
00131 
00132 void QwwColorComboBox::showPopup() {
00133 
00134     if (m_dlgEnabled)
00135         addItem("More");
00136     QComboBox::showPopup();
00137 }
00138 
00139 void QwwColorComboBox::popupDialog() {
00140     QColor newcol = QColorDialog::getColor(Qt::white, this);
00141     if (newcol.isValid()) {
00142         insertColor(count()-1, newcol, "Custom color");
00143         setCurrentIndex(count()-2);
00144     }
00145 }
00146 
00147 void QwwColorComboBox::_q_popup(const QModelIndex &ind) {
00148     QMessageBox::information(this, "POPUP", "POP");
00149     if (ind.row()==ind.model()->rowCount()-1) {
00150         popupDialog();
00151     }
00152 }
00153 
00154 bool QwwColorComboBox::eventFilter(QObject * o, QEvent * e) {
00155     if (o==view() && e->type()==QEvent::Hide) {
00156         if (m_dlgEnabled) {
00157             removeItem(count()-1);
00158         }
00159     }
00160     return QComboBox::eventFilter(o, e);
00161 }
00162 
00163 #endif

Generated on Sat Apr 21 21:54:35 2007 for wwWidgets by  doxygen 1.5.1