File: ColorSettingsWidget.cpp

package info (click to toggle)
subcommander 2.0.0~b5p1-2
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 14,880 kB
  • ctags: 9,003
  • sloc: cpp: 63,591; sh: 3,904; xml: 1,984; makefile: 1,166; ansic: 786; ruby: 251; lisp: 24
file content (139 lines) | stat: -rw-r--r-- 3,120 bytes parent folder | download | duplicates (4)
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
/* ====================================================================
 * Copyright (c) 2003-2007, Martin Hauner
 *                          http://subcommander.tigris.org
 *
 * Subcommander is licensed as described in the file doc/COPYING, which
 * you should have received as part of this distribution.
 * ====================================================================
 */

// sc
#include "config.h"
#include "ColorSettingsWidget.h"
#include "sublib/ExternButton.h"

// qt
#include <QtGui/QLayout>
#include <QtGui/QLabel>
#include <QtGui/QColor>
#include <QtGui/QColorDialog>
#include <QtGui/QTreeWidget>
#include <QtGui/QTreeWidgetItem>

// sys
#include <cassert>


/////////////////////////////////////////////////////////////////////


class ColorLvi : public QTreeWidgetItem
{
  typedef QTreeWidgetItem super;

public:
  ColorLvi( QTreeWidget* parent, const Color& color )
    : super(parent,QTreeWidgetItem::UserType), _color(color)
  {
    setText( 0, _color._name );
    setBackground( 1, _color._color );
  }

  virtual ~ColorLvi()
  {
  }

  const Color& getColor()
  {
    return _color;
  }

  void setColor( const Color& c )
  {
    _color = c;
    setBackground( 1, _color._color );
  }

private:
  Color _color;
};


/////////////////////////////////////////////////////////////////////

ColorSettingsWidget::ColorSettingsWidget( QWidget *parent )
: super(parent)
{
  QVBoxLayout* vl = new QVBoxLayout(this);
  vl->setContentsMargins(2,2,2,2);
  vl->setSpacing(5);
  {
    _clist = new QTreeWidget(this);
    _clist->setColumnCount(2);
    _clist->setHeaderLabels( QStringList() << _q("color type") << _q("color") );
    _clist->setRootIsDecorated(false);
    vl->addWidget(_clist);

    connect( 
      _clist, SIGNAL(itemDoubleClicked(QTreeWidgetItem*,int)),
      this,   SLOT(itemDoubleClicked(QTreeWidgetItem*,int)) );
    connect(
      _clist, SIGNAL(itemClicked(QTreeWidgetItem*,int)),
      this,   SLOT(itemClicked(QTreeWidgetItem*,int)) );

    _desc = new QLabel(this);
    vl->addWidget(_desc);
  }
}

ColorSettingsWidget::~ColorSettingsWidget()
{
}

void ColorSettingsWidget::itemDoubleClicked( QTreeWidgetItem* lvi, int column )
{
  ColorLvi* clvi  = dynamic_cast<ColorLvi*>(lvi);
  Color     color = clvi->getColor();
  QColor  choosen = QColorDialog::getColor( color._color, this );

  if( choosen.isValid() && choosen != color._color )
  {
    color._color = choosen;
    clvi->setColor(color);
    emit modified();
  }
}

void ColorSettingsWidget::itemClicked( QTreeWidgetItem* lvi, int column )
{
  ColorLvi* clvi  = dynamic_cast<ColorLvi*>(lvi);
  if( ! clvi )
  {
    return;
  }

  _desc->setText( clvi->getColor()._desc );
}


void ColorSettingsWidget::setColors( const Colors& colors )
{
  _clist->clear();

  for( Colors::const_iterator it = colors.begin(); it != colors.end(); it++ )
  {
    const Color& c = *it;
    new ColorLvi( _clist, c );
  }
}

void ColorSettingsWidget::getColors( Colors& colors )
{
  QTreeWidgetItemIterator it(_clist);
  while(*it)
  {
    ColorLvi* lvi = dynamic_cast<ColorLvi*>(*it);
    colors.push_back( lvi->getColor() );
    ++it;
  }
}