colorlistitem.h 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /*
  2. ktigcc - TIGCC IDE for KDE
  3. Copyright (C) 2006 Kevin Kofler
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software Foundation,
  14. Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #include <qlistbox.h>
  17. #include <qpainter.h>
  18. #include <qstyle.h>
  19. class ColorListItem : public QListBoxItem {
  20. public:
  21. ColorListItem(QListBox *listbox, const QColor &color)
  22. : QListBoxItem(listbox), m_color(color) {}
  23. virtual ~ColorListItem() {}
  24. QColor color() {return m_color;}
  25. void setColor(const QColor &color) {m_color=color; listBox()->update();}
  26. protected:
  27. virtual void paint(QPainter *painter) {
  28. // Find out whether we are painted onto our listbox.
  29. bool inListBox=listBox() && listBox()->viewport()==painter->device();
  30. QRect r(0,0,width(listBox()),height(listBox()));
  31. if (inListBox && isSelected())
  32. painter->eraseRect(r);
  33. painter->fillRect(2,2,width(listBox())-4,height(listBox())-4,m_color);
  34. if (inListBox && isCurrent())
  35. listBox()->style().drawPrimitive(QStyle::PE_FocusRect,painter,r,listBox()->colorGroup());
  36. }
  37. virtual int height(const QListBox *) const {return 16;}
  38. virtual int width(const QListBox *listBox) const {
  39. return listBox->contentsWidth();
  40. }
  41. private:
  42. QColor m_color;
  43. };