void KNMusicAlbumView::paintEvent(QPaintEvent *event) { Q_UNUSED(event) //Initial the painter. QPainter painter(viewport()); painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing | QPainter::SmoothPixmapTransform, true); //Get the option view item. QStyleOptionViewItem option=viewOptions(); //Check if we need to paint the background. if(autoFillBackground()) { painter.fillRect(rect(), option.palette.base()); } //Update the parameters of the view first. updateParameters(); //Check the model first. if(m_proxyModel==nullptr) { return; } //Get the row count. int albumCount=m_proxyModel->rowCount(); //Calculate the line count. m_lineCount=(albumCount+m_maxColumnCount-1)/m_maxColumnCount; //Check the album count first. if(albumCount==0) { return; } int currentColumn=0, currentLeft=m_spacing, currentLine=verticalScrollBar()->value()/m_itemSpacingHeight, //Skip the before. currentTop=m_spacing+currentLine*m_itemSpacingHeight, currentRow=currentLine*m_maxColumnCount, heightSurplus=height()+m_itemSpacingHeight; //Change the origin of coordinate. painter.translate(0, -verticalScrollBar()->value()); //Draw all the albums. while(currentRow < albumCount && heightSurplus > 0) { //Get the source index of the item. QModelIndex proxyIndex=m_proxyModel->index(currentRow, 0); //If the source index is not the current index, then draw the album. if(m_proxyModel->mapToSource(proxyIndex)!=m_selectedIndex) { paintAlbum(painter, QPoint(currentLeft, currentTop), proxyIndex); } //Add current row and column. currentRow++; currentColumn++; //Check if we need to move to next row. if(currentColumn>=m_maxColumnCount) { //Add one line. currentLine++; //Reset the column. currentColumn=0; //Change the position. currentLeft=m_spacing; currentTop+=m_itemSpacingHeight; heightSurplus-=m_itemSpacingHeight; } else { //Move to next column position. currentLeft+=m_itemSpacingWidth; } } //Update the scroll bar value. updateGeometries(); }
void KNMusicAlbumView::paintEvent(QPaintEvent *event) { Q_UNUSED(event) //Initial the painter. QPainter painter(viewport()); painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing | QPainter::SmoothPixmapTransform, true); //Check if we need to paint the background. if(autoFillBackground()) { painter.fillRect(rect(), viewOptions().palette.base()); } //Check the model first. if(m_proxyModel==nullptr) { return; } //Get the row count. int albumCount=m_proxyModel->rowCount(); //Check the album count first. if(albumCount==0) { return; } //Calculate the line count. m_lineCount=(albumCount+m_maxColumnCount-1)/m_maxColumnCount; //Generate the parameters. int currentColumn=0, currentLeft=m_spacing, currentLine=verticalScrollBar()->value()/m_itemSpacingHeight, currentTop=m_spacing+currentLine*m_itemSpacingHeight, currentRow=currentLine*m_maxColumnCount, heightSurplus=height()+m_itemSpacingHeight; /* * currentColumn * | * +-------------------------------------------------+ * | +---+ +---+ +---+ +---+ +---+ +---+ +---+ +---+ | * | | | | | | | | | | | | | | | | | | * | +---+ +---+ +---+ +---+ +---+ +---+ +---+ +---+ | * | | * | +---+ +---+ xxxxx-currentTop | * | | | | | xxxxx | - currentLine * | +---+ +---+ xxxxx | * | | | | -+ * | currentLeft-+ | | | * | +--QModelIndex(25252, 0) | | * | | | | * | currentRow | +-heightSurplus * | | | * | | | * | | | * +-------------------------------------------------+ -+ */ //Change the origin of coordinate. painter.translate(0, -verticalScrollBar()->value()); //Draw all the albums, until there's no album left, no height surplus. while(currentRow < albumCount && heightSurplus > 0) { //Get the source index of the item. QModelIndex &&proxyIndex=m_proxyModel->index(currentRow, 0); //If the source index is not the current index, then draw the album. if(m_proxyModel->mapToSource(proxyIndex)!=m_selectedIndex) { //Draw the unselect album. paintAlbum(painter, currentLeft, currentTop, proxyIndex); } //Add current row to the next album model row. ++currentRow; //Add current column to the next position. ++currentColumn; //Check if we need to move to next row. if(currentColumn>=m_maxColumnCount) { //Add current line. ++currentLine; //Reset the column. currentColumn=0; //Change the position. currentLeft=m_spacing; currentTop+=m_itemSpacingHeight; heightSurplus-=m_itemSpacingHeight; } else { //Move to next column position. currentLeft+=m_itemSpacingWidth; } } //Update the scroll bar value. updateGeometries(); }