OSDN Git Service

refactoring.
[kita/kita.git] / kita / src / threadlistview.cpp
1 /***************************************************************************
2 *   Copyright (C) 2003 by Hideki Ikemoto                                  *
3 *   ikemo@wakaba.jp                                                       *
4 *                                                                         *
5 *   This program is free software; you can redistribute it and/or modify  *
6 *   it under the terms of the GNU General Public License as published by  *
7 *   the Free Software Foundation; either version 2 of the License, or     *
8 *   (at your option) any later version.                                   *
9 ***************************************************************************/
10
11 #include "threadlistview.h"
12
13 #include <kiconloader.h>
14 #include <klistview.h>
15 #include <klocale.h>
16 #include <kurl.h>
17
18 #include <qtoolbutton.h>
19 #include <qcombobox.h>
20 #include <qregexp.h>
21 #include <qheader.h>
22
23 #include "libkita/thread.h"
24 #include "libkita/kitaconfig.h"
25
26 using namespace Kita;
27
28 ThreadListView::ThreadListView( QWidget* parent, const char* name )
29         : ThreadListViewBase( parent, name )
30 {
31     SearchButton->setPixmap( SmallIcon( "find" ) );
32     HideButton->setPixmap( SmallIcon( "filter" ) );
33     ReloadButton->setPixmap( SmallIcon( "reload" ) );
34
35     subjectList->addColumn( "" );
36     subjectList->addColumn( i18n( "No." ) );
37     subjectList->addColumn( "" );
38     subjectList->addColumn( i18n( "Title" ) );
39     subjectList->addColumn( i18n( "ResNum" ) );
40     subjectList->addColumn( i18n( "ReadNum" ) );
41     subjectList->addColumn( i18n( "Unread" ) );
42     subjectList->addColumn( i18n( "Since" ) );
43
44     QHeader *h = subjectList->header();
45     h->setResizeEnabled( FALSE, Col_Mark );
46     h->setResizeEnabled( FALSE, Col_Icon );
47     h->setStretchEnabled( TRUE, Col_Subject );
48
49     //subjectList->setColumnWidth( Col_Subject, 400 );
50     subjectList->setColumnWidthMode( Col_Subject, QListView::Manual );
51
52     connect( SearchButton, SIGNAL( clicked() ),
53              SLOT( slotSearchButton() ) );
54     connect( SearchCombo, SIGNAL( activated( int ) ),
55              SLOT( slotSearchButton() ) );
56     connect( HideButton, SIGNAL( toggled( bool ) ),
57              SLOT( slotHideButton( bool ) ) );
58     connect( subjectList, SIGNAL( mouseButtonClicked( int, QListViewItem*, const QPoint&, int ) ),
59              SLOT( slotMouseButtonClicked( int, QListViewItem* ) ) );
60     connect( this, SIGNAL( bookmarked( const QString&, bool ) ),
61              Kita::SignalCollection::getInstance(), SIGNAL( bookmarked( const QString&, bool ) ) );
62 }
63
64 ThreadListView::~ThreadListView()
65 {}
66
67 void ThreadListView::slotSearchButton()
68 {
69     insertSearchCombo();
70     QStringList list = parseSearchQuery( SearchCombo->currentText() );
71     searchNext( list );
72 }
73
74 void ThreadListView::insertSearchCombo()
75 {
76     for ( int count = 0; count < SearchCombo->count(); ++count ) {
77         if ( SearchCombo->text( count ) == SearchCombo->currentText() ) {
78             return ;
79         }
80     }
81     SearchCombo->insertItem( SearchCombo->currentText() );
82 }
83
84 QStringList ThreadListView::parseSearchQuery( const QString &input )
85 {
86     QStringList tmp = QStringList::split( ' ', input );
87     QStringList ret_list;
88     QRegExp truncSpace( "\\s*$" );
89     QStringList::iterator it = tmp.begin();
90
91     for ( ; it != tmp.end(); ++it ) {
92         ret_list += ( *it ).replace( truncSpace, "" );
93     }
94     return ret_list;
95 }
96
97 void ThreadListView::searchNext( const QStringList &query )
98 {
99     if ( query.isEmpty() ) return ;
100
101     if ( query != m_prevquery ) {
102         searchAll( query );
103         slotHideButton( HideButton->isOn() );
104         m_nextHitIndex = 0; //A next jump-search target reset to '0'.
105         return ;
106     }
107
108     if ( m_nextHitIndex >= m_hitList.size() ) {
109         return ;
110     }
111
112     KListViewItem* item = m_hitList[ m_nextHitIndex ];
113     subjectList->ensureItemVisible( item );
114     subjectList->setSelected( item, true );
115     m_nextHitIndex++;
116     if ( m_nextHitIndex >= m_hitList.size() ) m_nextHitIndex = 0;
117 }
118
119 void ThreadListView::searchAll( const QStringList &query )
120 {
121     m_hitList.clear();
122     m_prevquery = query;
123
124     QListViewItemIterator listIt( subjectList );
125     while ( listIt.current() != 0 ) {
126         KListViewItem * item = static_cast<KListViewItem*>( listIt.current() );
127         item->setPixmap( Col_Icon, 0 );
128
129         QStringList::const_iterator queryIt = query.begin();
130         for ( ; queryIt != query.end(); ++queryIt ) {
131             if ( item->text( Col_Subject ).contains( *queryIt, false ) ) {
132                 item->setPixmap( Col_Icon, SmallIcon( "find" ) );
133                 m_hitList.append( item );
134                 break;
135             }
136         }
137         ++listIt;
138     }
139 }
140
141 void ThreadListView::slotHideButton( bool on )
142 {
143     if ( m_hitList.empty() ) return ;
144
145     QListViewItemIterator listIt( subjectList );
146     while ( listIt.current() != 0 ) {
147         KListViewItem * item = static_cast<KListViewItem *>( listIt.current() );
148         if ( on && ! item->pixmap( Col_Icon ) ) {
149             item->setVisible( false );
150         } else {
151             item->setVisible( true );
152         }
153         ++listIt;
154     }
155 }
156
157 void ThreadListView::slotMouseButtonClicked( int button, QListViewItem* item )
158 {
159     if ( ! item ) return ;
160
161     KURL datURL = item->text( Col_DatURL );
162
163     Kita::Thread* thread = Kita::Thread::getByURL( datURL.url() );
164
165     switch ( button ) {
166     case MidButton:
167         emit showThreadRequested( thread->datURL(), true );
168         break;
169     case LeftButton:
170         if ( KitaConfig::alwaysUseTab() ) {
171             emit showThreadRequested( thread->datURL(), true );
172         } else {
173             emit showThreadRequested( thread->datURL(), false );
174         }
175         break;
176     }
177 }
178
179 int ThreadListViewItem::compare( QListViewItem* i, int col, bool ascending ) const
180 {
181     switch ( col ) {
182     case Col_ResNum:
183     case Col_Read:
184     case Col_Unread:
185         return QString::localeAwareCompare( i->key( col, ascending ), key( col, ascending ) );
186     case Col_Mark:
187         return QString::localeAwareCompare( i->key( Col_MarkOrder, ascending ), key( Col_MarkOrder, ascending ) );
188     default:
189         return QString::localeAwareCompare( key( col, ascending ), i->key( col, ascending ) );
190     }
191 }
192
193 #include "threadlistview.moc"