OSDN Git Service

add Col_Mark order
[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
17 #include <qtoolbutton.h>
18 #include <qcombobox.h>
19 #include <qregexp.h>
20 #include <qheader.h>
21
22 using namespace Kita;
23
24 ThreadListView::ThreadListView( QWidget* parent, const char* name )
25         : ThreadListViewBase( parent, name )
26 {
27     SearchButton->setPixmap( SmallIcon( "find" ) );
28     HideButton->setPixmap( SmallIcon( "filter" ) );
29     ReloadButton->setPixmap( SmallIcon( "reload" ) );
30
31     subjectList->addColumn( "" );
32     subjectList->addColumn( i18n( "No." ) );
33     subjectList->addColumn( "" );
34     subjectList->addColumn( i18n( "Title" ) );
35     subjectList->addColumn( i18n( "ResNum" ) );
36     subjectList->addColumn( i18n( "ReadNum" ) );
37     subjectList->addColumn( i18n( "Unread" ) );
38     subjectList->addColumn( i18n( "Since" ) );
39     subjectList->addColumn( i18n( "Board" ) );
40
41     subjectList->setColumnWidth( Col_Subject, 400 );
42     subjectList->setColumnWidthMode( Col_Subject, QListView::Manual );
43
44     connect( SearchButton, SIGNAL( clicked() ),
45              SLOT( slotSearchButton() ) );
46     connect( SearchCombo, SIGNAL( activated( int ) ),
47              SLOT( slotSearchButton() ) );
48     connect( HideButton, SIGNAL( toggled( bool ) ),
49              SLOT( slotHideButton( bool ) ) );
50 }
51
52 ThreadListView::~ThreadListView()
53 {}
54
55 void ThreadListView::slotSearchButton()
56 {
57     insertSearchCombo();
58     QStringList list = parseSearchQuery( SearchCombo->currentText() );
59     searchNext( list );
60 }
61
62 void ThreadListView::insertSearchCombo()
63 {
64     for ( int count = 0; count < SearchCombo->count(); ++count ) {
65         if ( SearchCombo->text( count ) == SearchCombo->currentText() ) {
66             return ;
67         }
68     }
69     SearchCombo->insertItem( SearchCombo->currentText() );
70 }
71
72 QStringList ThreadListView::parseSearchQuery( const QString &input )
73 {
74     QStringList tmp = QStringList::split( ' ', input );
75     QStringList ret_list;
76     QRegExp truncSpace( "\\s*$" );
77     QStringList::iterator it = tmp.begin();
78
79     for ( ; it != tmp.end(); ++it ) {
80         ret_list += ( *it ).replace( truncSpace, "" );
81     }
82     return ret_list;
83 }
84
85 void ThreadListView::searchNext( const QStringList &query )
86 {
87     if ( query.isEmpty() ) return ;
88
89     if ( query != m_prevquery ) {
90         searchAll( query );
91         slotHideButton( HideButton->isOn() );
92         m_nextHitIndex = 0; //A next jump-search target reset to '0'.
93         return ;
94     }
95
96     if ( m_nextHitIndex >= m_hitList.size() ) {
97         return ;
98     }
99
100     KListViewItem* item = m_hitList[ m_nextHitIndex ];
101     subjectList->ensureItemVisible( item );
102     subjectList->setSelected( item, true );
103     m_nextHitIndex++;
104     if ( m_nextHitIndex >= m_hitList.size() ) m_nextHitIndex = 0;
105 }
106
107 void ThreadListView::searchAll( const QStringList &query )
108 {
109     m_hitList.clear();
110     m_prevquery = query;
111
112     QListViewItemIterator listIt( subjectList );
113     while ( listIt.current() != 0 ) {
114         KListViewItem * item = static_cast<KListViewItem*>( listIt.current() );
115         item->setPixmap( Col_Icon, 0 );
116
117         QStringList::const_iterator queryIt = query.begin();
118         for ( ; queryIt != query.end(); ++queryIt ) {
119             if ( item->text( Col_Subject ).contains( *queryIt, false ) ) {
120                 item->setPixmap( Col_Icon, SmallIcon( "find" ) );
121                 m_hitList.append( item );
122                 break;
123             }
124         }
125         ++listIt;
126     }
127 }
128
129 void ThreadListView::slotHideButton( bool on )
130 {
131     if ( m_hitList.empty() ) return ;
132
133     QListViewItemIterator listIt( subjectList );
134     while ( listIt.current() != 0 ) {
135         KListViewItem * item = static_cast<KListViewItem *>( listIt.current() );
136         if ( on && ! item->pixmap( Col_Icon ) ) {
137             item->setVisible( false );
138         } else {
139             item->setVisible( true );
140         }
141         ++listIt;
142     }
143 }
144
145 int ThreadListViewItem::compare( QListViewItem* i, int col, bool ascending ) const
146 {
147     switch ( col ) {
148     case Col_ResNum:
149     case Col_Read:
150     case Col_Unread:
151         return QString::localeAwareCompare( i->key( col, ascending ), key( col, ascending ) );
152     case Col_Mark:
153         return QString::localeAwareCompare( i->key( Col_MarkOrder, ascending ), key( Col_MarkOrder, ascending ) );
154     default:
155         return QString::localeAwareCompare( key( col, ascending ), i->key( col, ascending ) );
156     }
157 }
158
159 #include "threadlistview.moc"