~ubuntu-branches/ubuntu/intrepid/adept/intrepid-updates

« back to all changes in this revision

Viewing changes to adept/applicationswidget.h

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2008-10-16 17:18:45 UTC
  • mfrom: (1.1.1 squeeze)
  • Revision ID: james.westby@ubuntu.com-20081016171845-0b2oonrmxa9sybn9
Tags: 3.0~beta4ubuntu1
* Merge with Debian, remaining changes:
 - use kde4.mk
* Set manager disabled when database is read only

Show diffs side-by-side

added added

removed removed

Lines of Context:
8
8
#include <adept/packagelist.h>
9
9
#include <adept/util.h>
10
10
 
 
11
#include <adept/sidebar.h>
 
12
 
11
13
#include <KLineEdit>
12
14
#include <KIcon>
13
15
#include <KLocale>
77
79
    virtual ~IconPolicy() {}
78
80
};
79
81
 
80
 
class ApplicationsPageModel : public KPageModel
81
 
{
82
 
    Q_OBJECT
83
 
public:
84
 
    typedef std::map< std::string, PackageListView * > Pages;
85
 
    typedef std::pair< std::string, PackageListView * > Pair;
86
 
    typedef std::vector< Pair > PageVec;
87
 
    IconPolicy iconPol;
88
 
 
89
 
protected:
90
 
    PageVec pages;
91
 
public:
92
 
    explicit ApplicationsPageModel( QObject *parent = 0 ) 
93
 
        : KPageModel( parent )
94
 
    {}
95
 
 
96
 
    virtual QModelIndex index( int row, int column,
97
 
                               const QModelIndex & = QModelIndex()
98
 
        ) const
99
 
    {
100
 
        return createIndex( row, column, 0 );
101
 
    }
102
 
 
103
 
    bool valid( const QModelIndex &i ) const {
104
 
        return i.row() < static_cast< int >( pages.size() ) && i.column() == 0;
105
 
    }
106
 
 
107
 
    std::string name( const QModelIndex &i ) {
108
 
        if ( !valid( i ) )
109
 
            return std::string();
110
 
        return pages[ i.row() ].first;
111
 
    }
112
 
 
113
 
    QVariant data( const QModelIndex &i, int role = Qt::DisplayRole ) const
114
 
    {
115
 
        if ( !valid( i ) )
116
 
            return QVariant();
117
 
        Pair p = pages[ i.row() ];
118
 
        switch (role) {
119
 
            case Qt::DisplayRole:
120
 
            case HeaderRole: return QVariant::fromValue( u8( p.first ) );
121
 
            case WidgetRole: return QVariant::fromValue(
122
 
                static_cast< QWidget * >( p.second ) );
123
 
            case Qt::DecorationRole: return QVariant::fromValue(
124
 
                QIcon( KIcon( iconPol.iconForGroup( u8( p.first ) ) ) ) );
125
 
            default: return QVariant();
126
 
        }
127
 
    }
128
 
 
129
 
    QModelIndex parent( const QModelIndex & ) const {
130
 
        return QModelIndex();
131
 
    }
132
 
 
133
 
    int columnCount( const QModelIndex & ) const {
134
 
        return 1;
135
 
    }
136
 
 
137
 
    int rowCount( const QModelIndex &a ) const {
138
 
        if ( a == QModelIndex() )
139
 
            return pages.size();
140
 
        return 0;
141
 
    }
142
 
 
143
 
    QModelIndex nameIndex( std::string n ) const {
144
 
        int j = 0;
145
 
        for ( PageVec::const_iterator i = pages.begin(); i != pages.end(); ++i )
146
 
        {
147
 
            if ( i->first == n )
148
 
                return createIndex( j, 0, 0 );
149
 
            ++ j;
150
 
        }
151
 
        return createIndex( 0, 0, 0 );
152
 
    }
153
 
 
154
 
    void update( const Pages &visible ) {
155
 
        layoutAboutToBeChanged();
156
 
        pages.clear();
157
 
        std::copy( visible.begin(), visible.end(), std::back_inserter( pages ) );
158
 
        dataChanged( createIndex( 0, 0, 0 ),
159
 
                     createIndex( pages.size() - 1, 1, 0 ) );
160
 
        layoutChanged();
161
 
    }
162
 
};
163
 
 
164
82
class ApplicationsWidget : public KVBox
165
83
{
166
84
    Q_OBJECT
167
85
 
168
 
    KPageView *pg;
169
86
    KLineEdit *m_searchLine;
170
 
    
 
87
    KHBox *hbox;
 
88
    SidebarListWidget *m_groups;
 
89
    PackageListView *m_list;
 
90
 
171
91
    PackageData &d;
172
 
    AppGroupPolicy groupPol;
173
 
    ApplicationsPageModel *pg_model;
174
 
    typedef std::map< std::string, PackageListView * > Pages;
175
92
    typedef std::map< std::string, TokenModel * > Models;
176
 
    Pages all, visible;
177
 
    Models models;
 
93
    Models all, visible;
 
94
    std::string empty, current;
 
95
 
178
96
    std::set< ept::Token > m_xapianSet;
179
97
    Validator validator;
180
98
 
 
99
    AppGroupPolicy groupPol;
 
100
    IconPolicy iconPol;
 
101
 
181
102
public:
182
103
 
183
104
    struct Matches {
206
127
        return ept::Token();
207
128
    }
208
129
 
 
130
    TokenModel *model( std::string g ) {
 
131
        if ( all.find( g ) == all.end() ) {
 
132
            TokenModel *m = new TokenModel();
 
133
            all.insert( std::make_pair( g, m ) );
 
134
        }
 
135
        return all.find( g )->second;
 
136
    }
 
137
 
209
138
    template< typename L >
210
139
    void fillList( L l ) {
211
140
        ProcessEvents::restart();
212
141
        QApplication::setOverrideCursor( QCursor( Qt::BusyCursor ) );
 
142
 
 
143
        for ( Models::iterator i = all.begin(); i != all.end(); ++i )
 
144
            i->second->rewind();
 
145
 
213
146
        visible.clear();
214
 
        std::string current, goal;
215
 
        pg->setUpdatesEnabled( false );
216
 
 
217
 
        goal = pg_model->name( pg->currentPage() );
218
 
 
219
 
        for ( Models::iterator i = models.begin(); i != models.end(); ++i )
220
 
            i->second->rewind();
221
147
 
222
148
        while ( !l.empty() ) {
223
 
            ProcessEvents::check( pg );
 
149
            ProcessEvents::check( this );
224
150
 
225
151
            ept::Token t = validate( l.head().token() );
226
152
 
232
158
            std::string g = l.head().template get< desktop::Group >();
233
159
            l = l.tail();
234
160
 
235
 
            if ( models.find( g ) == models.end() ) {
236
 
                TokenModel *m = new TokenModel();
237
 
                models.insert( std::make_pair( g, m ) );
238
 
            }
239
 
 
240
 
            if ( all.find( g ) == all.end() ) {
241
 
                PackageListView *w = setupList(
242
 
                    this, pg, models.find( g )->second, d );
243
 
                w->setSortingEnabled( true );
244
 
                w->hide();
245
 
                std::cerr << "Adding group: " << g << std::endl;
246
 
                all.insert( std::make_pair( g, w ) );
247
 
            }
248
 
            models.find( g )->second->appendToken( t );
 
161
            model( g )->appendToken( t );
249
162
 
250
163
            if ( visible.find( g ) == visible.end() ) {
251
 
                if ( current.empty() || (!goal.empty() && g == goal) ) {
252
 
                    std::cerr << "setting current to " << g << std::endl;
253
 
                    current = g;
254
 
                }
255
 
                visible.insert( visible.begin(), *all.find( g ) );
256
 
                updateVisible( current );
 
164
                visible[ g ] = model( g );
 
165
                // visible.insert( visible.begin(), model( g ) );
 
166
                updateVisible();
257
167
            }
258
 
 
259
168
        }
260
 
        for ( Models::iterator i = models.begin(); i != models.end(); ++i )
 
169
 
 
170
        setUpdatesEnabled( true );
 
171
 
 
172
        for ( Models::iterator i = all.begin(); i != all.end(); ++i )
261
173
            i->second->end();
262
 
        pg->setUpdatesEnabled( true );
263
174
        QApplication::restoreOverrideCursor();
264
 
    }
265
 
 
266
 
    void updateVisible( std::string current ) {
267
 
        pg_model->update( visible );
268
 
        for ( Pages::iterator i = all.begin(); i != all.end(); ++i ) {
269
 
            i->second->hide();
270
 
        }
271
 
        pg->setCurrentPage( pg_model->nameIndex( current ) );
272
 
        all.find( current )->second->show(); // show the widget...
 
175
 
 
176
        if ( !visible.count( current ) || current == empty ) {
 
177
            if ( !visible.empty() )
 
178
                current = visible.begin()->first;
 
179
            else
 
180
                current = empty;
 
181
        }
 
182
 
 
183
        QTimer::singleShot( 0, this, SLOT( updateVisible() ) );
 
184
    }
 
185
 
 
186
public Q_SLOTS:
 
187
    void updateVisible() {
 
188
        bool nocurrent = false;
 
189
        if ( visible.empty() ) {
 
190
            visible[ current ] = model( current );
 
191
        } else {
 
192
            if ( ! (visible.size() == 1 && visible.begin()->first == empty) )
 
193
                visible.erase( empty );
 
194
        }
 
195
        if ( !visible.count( current ) ) {
 
196
            visible[ current ] = model( current );
 
197
            nocurrent = true;
 
198
        }
 
199
        m_groups->clear();
 
200
        std::cerr << "-------------- (current = " << current << ") --"
 
201
                  << std::endl;
 
202
        for ( Models::iterator i = visible.begin(); i != visible.end(); ++i ) {
 
203
            std::cerr << "Adding " << i->first << std::endl;
 
204
            QIcon icon = QIcon( KIcon(
 
205
                                    iconPol.iconForGroup( u8( i->first ) ) ) );
 
206
            SidebarItem *it =
 
207
                new SidebarItem( 0, icon, u8( i->first ) );
 
208
            m_groups->addItem( it );
 
209
            if ( i->first == current ) {
 
210
                m_groups->setCurrentItem( it );
 
211
            }
 
212
        }
 
213
        if ( nocurrent )
 
214
            visible.erase( current );
 
215
        updateWidths();
 
216
        updateModel();
 
217
    }
 
218
 
 
219
public:
 
220
    void updateWidths() {
 
221
        int w = 0;
 
222
        for ( int i = 0; i < m_groups->count(); ++i )
 
223
        {
 
224
            QSize s = m_groups->sizeHintForIndex(
 
225
                m_groups->model()->index( i, 0 ) );
 
226
            if ( s.width() > w )
 
227
                w = s.width();
 
228
        }
 
229
        m_groups->setFixedWidth( w + 30 );
273
230
    }
274
231
 
275
232
    ApplicationsWidget( PackageData &_d, QWidget *p = 0 )
276
 
        : KVBox( p ), d( _d ), validator( _d )
 
233
        : KVBox( p ), d( _d ),
 
234
          empty( s8( i18n( "No matches found" ) ) ),
 
235
          validator( _d )
277
236
    {
278
237
        KGlobal::dirs()->addResourceDir(
279
238
            "desktopicon",
282
241
        m_searchLine = new KLineEdit( this );
283
242
        m_searchLine->setClearButtonShown( true );
284
243
        m_searchLine->setClickMessage( i18n( "Search applications" ) );
285
 
        pg = new KPageView( this );
286
 
        pg_model = new ApplicationsPageModel( this );
287
 
        pg->setModel( pg_model );
288
 
        pg->setFaceType( KPageView::List );
 
244
        hbox = new KHBox( this );
 
245
        m_groups = new SidebarListWidget( hbox );
 
246
 
 
247
        // FUNK
 
248
        SidebarDelegate *deleg = new SidebarDelegate( m_groups );
 
249
        deleg->setShowText( true ); // TODO: SETTINGS
 
250
        m_groups->setItemDelegate( deleg );
 
251
        m_groups->setIconSize( QSize( 48, 48 ) );
 
252
        m_groups->setUniformItemSizes( true );
 
253
        // END FUNK
 
254
 
 
255
        m_list = setupList( this, hbox, model( empty ), d );
 
256
 
 
257
        connect( m_groups, SIGNAL( itemClicked( QListWidgetItem* ) ),
 
258
                 this, SLOT( selectGroup( QListWidgetItem* ) ) );
289
259
        connect( m_searchLine, SIGNAL( returnPressed() ),
290
260
                 this, SLOT( search() ) );
 
261
 
 
262
        current = empty;
 
263
        updateVisible();
291
264
    }
292
265
 
293
266
Q_SIGNALS:
294
267
    void refreshSig();
295
268
 
296
269
public Q_SLOTS:
 
270
    void updateModel() {
 
271
        std::cerr << "updateModel: '" << current << "' -> " <<
 
272
            (void*) model( current ) << std::endl;
 
273
        m_list->setModel( model( empty ) ); // work around something that looks
 
274
                                            // like a QT bug to me
 
275
        m_list->setModel( model( current ) );
 
276
    }
 
277
 
 
278
    void selectGroup( QListWidgetItem *i ) {
 
279
        current = s8( i->text() );
 
280
        updateModel();
 
281
    }
 
282
 
297
283
    void search() {
298
284
        m_xapianSet.clear();
299
285
        list::output(