@ -7,17 +7,43 @@
# include <QPushButton>
# include <QPainter>
# include <QPixmap>
# include <QMenu>
# include <QMouseEvent>
/* QListView that prevents deselection */
/* QListView that prevents deselection, handles invite plus-button clicks */
class ChannelListView : public QListView {
Q_OBJECT
public :
using QListView : : QListView ;
signals :
void inviteClicked ( const QString & channelId ) ;
protected :
void mousePressEvent ( QMouseEvent * e ) override {
if ( indexAt ( e - > pos ( ) ) . isValid ( ) )
QListView : : mousePressEvent ( e ) ;
QModelIndex idx = indexAt ( e - > pos ( ) ) ;
if ( ! idx . isValid ( ) ) return ;
QRect plusRect = ChannelDelegate : : inviteButtonRect ( visualRect ( idx ) ) ;
if ( plusRect . contains ( e - > pos ( ) ) ) {
emit inviteClicked ( idx . data ( ChannelChannelIdRole ) . toString ( ) ) ;
return ;
}
QListView : : mousePressEvent ( e ) ;
}
void mouseMoveEvent ( QMouseEvent * e ) override {
int prevRow = m_hoveredInviteRow ;
m_hoveredInviteRow = - 1 ;
QModelIndex idx = indexAt ( e - > pos ( ) ) ;
if ( idx . isValid ( ) ) {
QRect plusRect = ChannelDelegate : : inviteButtonRect ( visualRect ( idx ) ) ;
if ( plusRect . contains ( e - > pos ( ) ) )
m_hoveredInviteRow = idx . row ( ) ;
}
if ( m_hoveredInviteRow ! = prevRow ) {
model ( ) - > setData ( model ( ) - > index ( prevRow , 0 ) , 0 , ChannelInviteBtnHoveredRole ) ;
model ( ) - > setData ( model ( ) - > index ( m_hoveredInviteRow , 0 ) , 1 , ChannelInviteBtnHoveredRole ) ;
}
setCursor ( m_hoveredInviteRow > = 0 ? Qt : : PointingHandCursor : Qt : : ArrowCursor ) ;
QListView : : mouseMoveEvent ( e ) ;
}
QItemSelectionModel : : SelectionFlags selectionCommand ( const QModelIndex & idx , const QEvent * e ) const override {
if ( idx . isValid ( ) )
@ -29,6 +55,9 @@ protected:
if ( selectionModel ( ) & & ! selectionModel ( ) - > hasSelection ( ) & & currentIndex ( ) . isValid ( ) )
selectionModel ( ) - > select ( currentIndex ( ) , QItemSelectionModel : : ClearAndSelect ) ;
}
private :
int m_hoveredInviteRow = - 1 ;
} ;
static QIcon makeChannelIcon ( const QColor & color , const QString & letter ) {
@ -70,7 +99,7 @@ ChannelList::ChannelList(DbManager* db, QWidget *parent)
m_listView - > setSelectionMode ( QAbstractItemView : : SingleSelection ) ;
m_listView - > setHorizontalScrollBarPolicy ( Qt : : ScrollBarAlwaysOff ) ;
m_listView - > setVerticalScrollMode ( QAbstractItemView : : ScrollPerPixel ) ;
m_listView - > setContextMenuPolicy ( Qt : : CustomContextMenu ) ;
m_listView - > setMouseTracking ( true ) ;
layout - > addWidget ( m_listView ) ;
auto * footer = new QWidget ( this ) ;
@ -119,8 +148,8 @@ ChannelList::ChannelList(DbManager* db, QWidget *parent)
emit channelSelected ( cid ) ;
}
} ) ;
connect ( m_listView , & QListView : : customContextMenuRequest ed,
this , & ChannelList : : onContextMenu ) ;
connect ( static_cast < ChannelListView * > ( m_listView ) , & ChannelListView : : inviteClick ed,
this , & ChannelList : : inviteRequested ) ;
}
void ChannelList : : loadChannels ( ) {
@ -174,16 +203,6 @@ void ChannelList::selectChannel(const QString& channelId) {
}
}
void ChannelList : : onContextMenu ( const QPoint & pos ) {
QModelIndex index = m_listView - > indexAt ( pos ) ;
if ( ! index . isValid ( ) ) return ;
QString cid = index . data ( ChannelChannelIdRole ) . toString ( ) ;
QMenu menu ( this ) ;
QAction * inviteAction = menu . addAction ( " Пригласить " ) ;
if ( menu . exec ( m_listView - > viewport ( ) - > mapToGlobal ( pos ) ) = = inviteAction )
emit inviteRequested ( cid ) ;
}
void ChannelList : : setChannelPeersOnline ( const QString & channelId , int count ) {
for ( int row = 0 ; row < m_model - > rowCount ( ) ; row + + ) {
QStandardItem * item = m_model - > item ( row ) ;
@ -202,3 +221,5 @@ void ChannelList::setAutoConnectActive(bool active) {
if ( item ) item - > setData ( active ? 1 : 0 , ChannelAutoConnectActiveRole ) ;
}
}
# include "channellist.moc"