@ -51,7 +51,7 @@ protected:
}
QItemSelectionModel : : SelectionFlags selectionCommand ( const QModelIndex & idx , const QEvent * e ) const override {
if ( idx . isValid ( ) )
return QItemSelectionModel : : ClearAndSelect | QItemSelectionModel : : Current ;
return QItemSelectionModel : : ClearAndSelect ;
return QItemSelectionModel : : NoUpdate ;
}
void selectionChanged ( const QItemSelection & sel , const QItemSelection & desel ) override {
@ -158,53 +158,52 @@ ChannelList::ChannelList(DbManager* db, QWidget *parent)
}
void ChannelList : : loadChannels ( ) {
QModelIndex curIdx = m_listView - > currentIndex ( ) ;
QString selectedCid ;
int selectedRow = - 1 ;
if ( curIdx . isValid ( ) ) {
selectedCid = curIdx . data ( ChannelChannelIdRole ) . toString ( ) ;
selectedRow = curIdx . row ( ) ;
}
if ( m_listView - > currentIndex ( ) . isValid ( ) )
selectedCid = m_listView - > currentIndex ( ) . data ( ChannelChannelIdRole ) . toString ( ) ;
auto * sel = m_listView - > selectionModel ( ) ;
sel - > blockSignals ( true ) ;
m_model - > clear ( ) ;
if ( ! m_db | | ! m_db - > isOpen ( ) ) return ;
auto channels = m_db - > getChannels ( ) ;
static const QColor colors [ ] = {
QColor ( 74 , 144 , 226 ) , QColor ( 126 , 211 , 33 ) , QColor ( 245 , 166 , 35 ) ,
QColor ( 208 , 2 , 27 ) , QColor ( 144 , 19 , 254 ) , QColor ( 0 , 184 , 148 )
} ;
int ci = 0 ;
for ( const auto & ch : channels ) {
auto letter = ch . name . mid ( 0 , 1 ) . toUpper ( ) ;
if ( letter . isEmpty ( ) ) letter = " # " ;
auto * item = new QStandardItem ( ) ;
item - > setData ( ch . name , ChannelName Role) ;
item - > setData ( ch . channelId , ChannelChannelId Role) ;
item - > setData ( QString ( ) , ChannelLastMessage Role) ;
item - > setData ( makeChannelIcon ( colors [ ci % 6 ] , letter ) , ChannelIconRole ) ;
m_model - > appendRow ( item ) ;
DEBUG_INFO ( DEBUG_CATEGORY_DEBUG , " ChannelList::loadChannels row=%d cid='%s' name='%s' " ,
m_model - > rowCount ( ) - 1 , qPrintable ( ch . channelId ) , qPrintable ( ch . name ) ) ;
ci + + ;
if ( m_db & & m_db - > isOpen ( ) ) {
auto channels = m_db - > getChannels ( ) ;
static const QColor colors [ ] = {
QColor ( 74 , 144 , 226 ) , QColor ( 126 , 211 , 33 ) , QColor ( 245 , 166 , 35 ) ,
QColor ( 208 , 2 , 27 ) , QColor ( 144 , 19 , 254 ) , QColor ( 0 , 184 , 148 )
} ;
int ci = 0 ;
for ( const auto & ch : channels ) {
auto letter = ch . name . mid ( 0 , 1 ) . toUpper ( ) ;
if ( letter . isEmpty ( ) ) letter = " # " ;
auto * item = new QStandardItem ( ) ;
item - > setData ( ch . name , ChannelNameRole ) ;
item - > setData ( ch . channelId , ChannelChannelId Role) ;
item - > setData ( QString ( ) , ChannelLastMessage Role) ;
item - > setData ( makeChannelIcon ( colors [ ci % 6 ] , letter ) , ChannelIcon Role) ;
m_model - > appendRow ( item ) ;
DEBUG_INFO ( DEBUG_CATEGORY_DEBUG , " ChannelList::loadChannels row=%d cid='%s' name='%s' " ,
m_model - > rowCount ( ) - 1 , qPrintable ( ch . channelId ) , qPrintable ( ch . name ) ) ;
ci + + ;
}
}
sel - > blockSignals ( false ) ;
if ( ! selectedCid . isEmpty ( ) )
if ( ! selectedCid . isEmpty ( ) ) {
selectChannel ( selectedCid ) ;
if ( ! m_listView - > currentIndex ( ) . isValid ( ) ) {
int fallbackRow = ( selectedRow > = 0 ) ? selectedRow : 0 ;
if ( fallbackRow > = m_model - > rowCount ( ) )
fallbackRow = m_model - > rowCount ( ) - 1 ;
if ( fallbackRow > = 0 )
m_listView - > setCurrentIndex ( m_model - > index ( fallbackRow , 0 ) ) ;
} else if ( m_model - > rowCount ( ) > 0 ) {
QModelIndex idx = m_model - > index ( 0 , 0 ) ;
sel - > select ( idx , QItemSelectionModel : : ClearAndSelect ) ;
sel - > setCurrentIndex ( idx , QItemSelectionModel : : NoUpdate ) ;
}
}
void ChannelList : : selectChannel ( const QString & channelId ) {
for ( int row = 0 ; row < m_model - > rowCount ( ) ; row + + ) {
if ( m_model - > item ( row ) - > data ( ChannelChannelIdRole ) . toString ( ) = = channelId ) {
m_listView - > setCurrentIndex ( m_model - > index ( row , 0 ) ) ;
QModelIndex idx = m_model - > index ( row , 0 ) ;
m_listView - > selectionModel ( ) - > select ( idx , QItemSelectionModel : : ClearAndSelect ) ;
m_listView - > selectionModel ( ) - > setCurrentIndex ( idx , QItemSelectionModel : : NoUpdate ) ;
return ;
}
}