colorlistitem.h 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /*
  2. ktigcc - TIGCC IDE for KDE
  3. Copyright (C) 2006-2007 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. #pragma once
  17. #include <Q3ListBox>
  18. #include <QPainter>
  19. #include <QStyle>
  20. #include <QStyleOptionFocusRect>
  21. class ColorListItem : public Q3ListBoxItem {
  22. public:
  23. ColorListItem(Q3ListBox *listbox, const QColor &color)
  24. : Q3ListBoxItem(listbox), m_color(color) {}
  25. virtual ~ColorListItem() {}
  26. QColor color() {return m_color;}
  27. void setColor(const QColor &color) {m_color=color; listBox()->update();}
  28. protected:
  29. virtual void paint(QPainter *painter) {
  30. // Find out whether we are painted onto our listbox.
  31. bool inListBox=listBox() && listBox()->viewport()==painter->device();
  32. QRect r(0,0,width(listBox()),height(listBox()));
  33. if (inListBox && isSelected())
  34. painter->eraseRect(r);
  35. painter->fillRect(2,2,width(listBox())-4,height(listBox())-4,m_color);
  36. if (inListBox && isCurrent()) {
  37. QStyleOptionFocusRect opt;
  38. opt.rect.setRect(0,0,width(listBox()),height(listBox()));
  39. opt.palette=listBox()->palette();
  40. opt.state=QStyle::State_FocusAtBorder;
  41. if (isSelected())
  42. opt.backgroundColor=opt.palette.highlight().color();
  43. else
  44. opt.backgroundColor=opt.palette.base().color();
  45. listBox()->style()->drawPrimitive(QStyle::PE_FrameFocusRect,&opt,painter,listBox());
  46. }
  47. }
  48. virtual int height(const Q3ListBox *) const {return 16;}
  49. virtual int width(const Q3ListBox *listBox) const {
  50. return listBox->contentsWidth();
  51. }
  52. private:
  53. QColor m_color;
  54. };