summaryrefslogtreecommitdiffstats
path: root/src/common/gui/list_view.cpp
blob: 8fbeee2785716aae46496be58abeb76967c0709c (plain)
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/***************************************************************************
 *   Copyright (C) 2006 Nicolas Hadacek <hadacek@kde.org>                  *
 *   Copyright (C) 1992-2003 Trolltech AS.                                 *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 ***************************************************************************/
#include "list_view.h"

#include <tqapplication.h>
#include <tqpainter.h>
#include <tqlineedit.h>
#include <tqheader.h>
#include <tqmetaobject.h>
#include <tqvariant.h>

//----------------------------------------------------------------------------
ListView::ListView(TQWidget *parent, const char *name)
  : TDEListView(parent, name)
{
  TQToolTip::remove(this);
  _tooltip = new ListViewToolTip(this);
}

ListView::~ListView()
{
  delete _tooltip;
}

TQString ListView::tooltip(const TQListViewItem &, int) const
{
  return TQString();
}

void ListView::clear()
{
  _editItems.clear();
  TDEListView::clear();
}

bool ListView::eventFilter(TQObject *o, TQEvent *e)
{
  TQValueList<EditListViewItem *>::const_iterator it;
  for (it=_editItems.begin(); it!=_editItems.end(); ++it) {
    for (uint i=0; i<(*it)->_editWidgets.count(); i++) {
      if ( (*it)->_editWidgets[i]==o ) {
        //tqDebug("event %i", e->type());
        switch (e->type()) {
          case TQEvent::KeyPress: {
            TQKeyEvent *ke = static_cast<TQKeyEvent*>(e);
            switch (ke->key()) {
              case Key_Enter:
              case Key_Return:
                (*it)->renameDone(true);
                return true;
              case Key_Escape:
                (*it)->removeEditBox();
                return true;
            }
            break;
          }
          case TQEvent::FocusOut: {
            //tqDebug("focus out %i %i=%i", tqApp->focusWidget(), focusWidget(), (*it)->_editWidgets[i]);
            if ( tqApp->focusWidget() && focusWidget()==(*it)->_editWidgets[i] ) break;
            //tqDebug("ext");
            TQCustomEvent *e = new TQCustomEvent(9999);
            TQApplication::postEvent(o, e);
            return true;
          }
          case 9999:
            (*it)->renameDone(false);
            return true;
          default:
            //tqDebug("  ignored");
            break;
        }
      }
    }
  }
  return TDEListView::eventFilter(o, e);
}

void ListView::stopRenaming(bool force)
{
  TQValueList<EditListViewItem *>::const_iterator it;
  for (it=_editItems.begin(); it!=_editItems.end(); ++it)
    if ( (*it)->isRenaming() ) (*it)->renameDone(force);
}

//----------------------------------------------------------------------------
void ListViewToolTip::maybeTip(const TQPoint &p)
{
  if ( _listView==0 ) return;
  const TQListViewItem* item = _listView->itemAt(p);
  if ( item==0 ) return;
  TQRect rect = _listView->itemRect(item);
  if ( !rect.isValid() ) return;
  int col = _listView->header()->sectionAt(p.x());
  TQString text = _listView->tooltip(*item, col);
  if ( !text.isEmpty() ) {
    int hpos = _listView->header()->sectionPos(col);
    rect.setLeft(hpos);
    rect.setRight(hpos + _listView->header()->sectionSize(col));
    tip(rect, text);
  }
}

//----------------------------------------------------------------------------
EditListViewItem::EditListViewItem(ListView *list)
  : TDEListViewItem(list), _renaming(false)
{
  setRenameEnabled(0, true);
  list->_editItems.append(this);
}

EditListViewItem::EditListViewItem(TDEListViewItem *item)
  : TDEListViewItem(item), _renaming(false)
{
  setRenameEnabled(0, true);
  static_cast<ListView *>(listView())->_editItems.append(this);
}

EditListViewItem::~EditListViewItem()
{
  if ( listView() ) static_cast<ListView *>(listView())->_editItems.remove(this);
  for (uint i=0; i<_editWidgets.count(); i++) delete _editWidgets[i];
}

void EditListViewItem::paintCell(TQPainter *p, const TQColorGroup &cg, int column, int width, int align)
{
  if ( column<int(_editWidgets.count()) && _editWidgets[column] )
    p->fillRect(0, 0, width, height(), cg.color(TQColorGroup::Background));
  else TDEListViewItem::paintCell(p, cg, column, width, align);
}

void EditListViewItem::startRename()
{
  if ( !renameEnabled(0) ) return;
  TQListView *lv = listView();
  if ( !lv ) return;
  TDEListViewItem::startRename(0);
  if (renameBox) {
    renameBox->removeEventFilter(lv);
    renameBox->hide();
    lv->removeChild(renameBox);
  }
  _renaming = true;
  _editWidgets.resize(lv->columns());
  for (uint i=0; i<_editWidgets.count(); i++) {
    TQRect r = lv->itemRect(this);
    r = TQRect(lv->viewportToContents(r.topLeft()), r.size());
    r.setLeft(lv->header()->sectionPos(i));
    r.setWidth(lv->header()->sectionSize(i) - 1);
    if ( i==0 ) r.setLeft(r.left() + lv->itemMargin() + (depth() + (lv->rootIsDecorated() ? 1 : 0)) * lv->treeStepSize() - 1);
    if ( (lv->contentsX() + lv->visibleWidth())<(r.x() + r.width()) )
      lv->scrollBy((r.x() + r.width() ) - ( lv->contentsX() + lv->visibleWidth() ), 0);
    if ( r.width()>lv->visibleWidth() ) r.setWidth(lv->visibleWidth());

    _editWidgets[i] = editWidgetFactory(i);
    if ( _editWidgets[i]==0 ) continue;
    _editWidgets[i]->installEventFilter(lv);
    lv->addChild(_editWidgets[i], r.x(), r.y());
    uint w = TQMIN(r.width(), _editWidgets[i]->sizeHint().width());
    _editWidgets[i]->resize(w, r.height());
    lv->viewport()->setFocusProxy(_editWidgets[i]);
    _editWidgets[i]->setFocus();
    _editWidgets[i]->show();
  }
}

void EditListViewItem::removeEditBox()
{
  TQListView *lv = listView();
  if ( !lv ) return;
  _renaming = false;
  bool resetFocus = false;
  for (uint i=0; i<_editWidgets.count(); i++) {
    if ( lv->viewport()->focusProxy()==_editWidgets[i] ) resetFocus = true;
    delete _editWidgets[i];
  }
  _editWidgets.clear();
  delete renameBox;
  renameBox = 0;
  if (resetFocus) {
    lv->viewport()->setFocusProxy(lv);
    lv->setFocus();
  }
}

void EditListViewItem::editDone(int col, const TQWidget *edit)
{
  if ( edit->metaObject()->findProperty("text", true)!=-1 )
    emit listView()->itemRenamed(this, col, edit->property("text").toString());
  else if ( edit->metaObject()->findProperty("currentText", true)!=-1 )
    emit listView()->itemRenamed(this, col, edit->property("currentText").toString());
}

void EditListViewItem::renameDone(bool force)
{
  TQListView *lv = listView();
  if ( !lv || !_renaming ) return;
  _renaming = false;
  for (uint i=0; i<_editWidgets.count(); i++) {
    if ( !force && !alwaysAcceptEdit(i) ) continue;
    emit lv->itemRenamed(this, i);
    if ( _editWidgets[i] ) editDone(i, _editWidgets[i]);
  }
  removeEditBox();
}

int EditListViewItem::width(const TQFontMetrics &fm, const TQListView *lv, int col) const
{
  int w = TDEListViewItem::width(fm, lv, col);
  TQWidget *edit = editWidgetFactory(col);
  if ( edit==0 ) return w;
  w = TQMAX(w, edit->sizeHint().width());
  delete edit;
  return w;
}