summaryrefslogtreecommitdiffstats
path: root/krusader/ActionMan/useractionlistview.cpp
blob: 05340276dc81a14f225cbf7481c6eca6b67ffaca (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
222
223
224
225
226
227
228
229
230
231
232
//
// C++ Implementation: useractionlistview
//
// Description: 
//
//
// Author: Jonas Bähr, (C) 2006
//
// Copyright: See COPYING file that comes with this distribution
//
//
#include "useractionlistview.h"

#include <klocale.h>
#include <kiconloader.h>
#include <qptrlist.h>
#include <qdom.h>

#include "../krusader.h"
#include "../UserAction/kraction.h"
#include "../UserAction/useraction.h"

#define COL_TITLE	0
#define COL_NAME	1


//////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////     UserActionListView    /////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////

UserActionListView::UserActionListView( QWidget * parent, const char * name )
 : KListView( parent, name )
{
   addColumn( i18n("Title") );
   //addColumn( i18n("Identifier") );
   setResizeMode( QListView::AllColumns );

   setRootIsDecorated( true );
   setSelectionMode( QListView::Extended ); // normaly select single items but one may use Ctrl or Shift to select multiple
   setSorting( COL_TITLE );

   update();
}

UserActionListView::~UserActionListView()
{
}

QSize UserActionListView::sizeHint() const {
   return QSize(200, 400);
}


void UserActionListView::update() {
   clear();
   UserAction::KrActionList list = krUserAction->actionList();
   for ( KrAction* action = list.first(); action; action = list.next() )
      insertAction( action );
   //sort(); // this is done automaticly
}

void UserActionListView::update( KrAction* action ) {
   UserActionListViewItem* item = findActionItem( action );
   if ( item ) {
      // deleting & re-inserting is _much_easyer then tracking all possible cases of category changes!
      bool current = ( item == currentItem() );
      bool selected = item->isSelected();
      delete item;
      item = insertAction( action );
      if ( current )
         setCurrentItem( item );
      if ( selected )
         setSelected( item, true );
   }
}

UserActionListViewItem* UserActionListView::insertAction( KrAction* action ) {
   if ( ! action )
      return 0;

   UserActionListViewItem* item;

   if ( action->category().isEmpty() )
      item = new UserActionListViewItem( this, action );
   else {
      QListViewItem* categoryItem = findCategoryItem( action->category() );
      if ( ! categoryItem ) {
         categoryItem = new KListViewItem( this, action->category() ); // create the new category item it not already present
         categoryItem->setSelectable( false );
      }
      item = new UserActionListViewItem( categoryItem, action );
   }

   item->setAction( action );
   return item;
}

QListViewItem* UserActionListView::findCategoryItem( const QString& category ) {
   for ( QListViewItem* item = firstChild(); item; item = item->nextSibling() )
      if ( item->text( COL_TITLE ) == category && item->text( COL_NAME ).isEmpty() ) // because actions must have a name, items without name haveto be categories
         return item;

   return 0;
}

UserActionListViewItem* UserActionListView::findActionItem( const KrAction* action ) {
   for ( QListViewItemIterator it( this ); it.current(); ++it ) {
      if ( UserActionListViewItem* item = dynamic_cast<UserActionListViewItem*>( it.current() ) ) {
         if ( item->action() == action )
            return item;
      }
   } //for
   return 0;
}

KrAction * UserActionListView::currentAction() const {
   if ( UserActionListViewItem* item = dynamic_cast<UserActionListViewItem*>( currentItem() ) )
      return item->action();
   else
      return 0;
}

void UserActionListView::setCurrentAction( const KrAction* action) {
   UserActionListViewItem* item = findActionItem( action );
   if ( item ) {
      setCurrentItem( item );
//       setSelected( item, true );
//       repaintItem( item );
   }
}

void UserActionListView::setFirstActionCurrent() {
  for ( QListViewItemIterator it( this ); it.current(); ++it ) {
    if ( UserActionListViewItem* item = dynamic_cast<UserActionListViewItem*>( it.current() ) ) {
      setCurrentItem( item );
      break;
    }
  } //for
}

void UserActionListView::setCurrentItem( QListViewItem* item ) {
   if ( ! item )
      return;
   ensureItemVisible( item );
   QListView::setCurrentItem( item );
}

QDomDocument UserActionListView::dumpSelectedActions( QDomDocument* mergeDoc ) const {
   QPtrList<QListViewItem> list = selectedItems();
   QDomDocument doc;
   if ( mergeDoc )
      doc = *mergeDoc;
   else
      doc = UserAction::createEmptyDoc();
   QDomElement root = doc.documentElement();

   for ( QListViewItem* item = list.first(); item; item = list.next() )
      if ( UserActionListViewItem* actionItem = dynamic_cast<UserActionListViewItem*>( item ) )
         root.appendChild( actionItem->action()->xmlDump( doc ) );

   return doc;
}

void UserActionListView::removeSelectedActions() {
   QPtrList<QListViewItem> list = selectedItems();

   for ( QListViewItem* item = list.first(); item; item = list.next() )
      if ( UserActionListViewItem* actionItem = dynamic_cast<UserActionListViewItem*>( item ) ) {
         delete actionItem->action(); // remove the action itself
         delete actionItem; // remove the action from the list
      } // if

}

//////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////     UserActionListViewItem    ////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////

UserActionListViewItem::UserActionListViewItem( QListView* view, KrAction* action )
 : KListViewItem( view )
{
   setAction( action );
}

UserActionListViewItem::UserActionListViewItem( QListViewItem* item, KrAction * action )
 : KListViewItem( item )
{
   setAction( action );
}

UserActionListViewItem::~UserActionListViewItem() {
/*   // remove category-item if the last member ofthiscategory disappears
   if ( QListViewItem* item = dynamic_cast<QListViewItem*>( parent() ) ) {
      if ( item->childCount() <= 1 )
         item->deleteLater(); // not possible since not inherited from QObject
   }*/
}


void UserActionListViewItem::setAction( KrAction * action ) {
   if ( ! action )
      return;

   _action = action;
   update();
}

KrAction * UserActionListViewItem::action() const {
   return _action;
}

void UserActionListViewItem::update() {
   if ( ! _action )
      return;

   if ( ! _action->icon().isEmpty() )
      setPixmap( COL_TITLE, KGlobal::iconLoader()->loadIcon( _action->icon(), KIcon::Small ) );
   setText( COL_TITLE, _action->text() );
   setText( COL_NAME, _action->name() );
}

int UserActionListViewItem::compare( QListViewItem* i, int col, bool ascending ) const {
// FIXME some how this only produces bullshit :-/
//   if ( i->text( COL_NAME ).isEmpty() ) { // categories only have titles
//      //kdDebug() << "this->title: " << text(COL_TITLE) << " |=|   i->title: " << i->text(COL_TITLE)  << endl;
//       return ( ascending ? -1 : 1 ); // <0 means this is smaller then i
//    }
//    else
      return QListViewItem::compare( i, col, ascending );
}