123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- #include "baseheader.h"
- #include <Common/External/Core/PaintHelper/painthelper.h>
- #include <QDebug>
- #include <QApplication>
- BaseHeader::BaseHeader(QWidget *parent, Qt::Orientation orientation, Qt::Alignment flags)
- : QHeaderView(orientation, parent)
- , m_alignmentText(flags)
- , m_nCurSortedIndex(-1)
- , m_bSortable(false)
- , m_stateCurSorted(Disable)
- , m_bCheckable(false)
- , m_checkState(Qt::Unchecked)
- {
- connect(this, &QHeaderView::sectionClicked, this, &BaseHeader::OnSectionClicked);
- }
- void BaseHeader::SetTextAlignment(Qt::Alignment flags)
- {
- m_alignmentText = flags;
- }
- void BaseHeader::SetSortable(bool value, const QList<int> ¬SortList)
- {
- m_bSortable = value;
- setSectionsClickable(value);
- m_listSortDisable = notSortList;
- }
- void BaseHeader::SetCheckable(bool value)
- {
- m_bCheckable = value;
- setSectionsClickable(value);
- }
- void BaseHeader::SetChecked(Qt::CheckState state)
- {
- m_checkState = state;
- updateSection(0);
- }
- QString BaseHeader::GetText(int logicalIndex)
- {
- return model()->headerData(logicalIndex, orientation()).toString();
- }
- void BaseHeader::paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const
- {
- //颜色
- QColor colorBG = QColor(70, 70, 73);
- QColor colorBorder = QColor(255, 255, 255, 38);
- QColor colorText = QColor(210, 210, 210);
- //背景
- PainterEx *painterEx = static_cast<PainterEx*>(painter);
- painterEx->SetBrushOnly(colorBG);
- painterEx->drawRect(rect);
-
- //顶部横线
- painterEx->DrawBorder(rect, colorBorder, PainterEx::RectBorderTop);
- //文本
- FontEx font(qApp->font().family(), 14, false);
- QFontMetrics fontMetrics(font);
- QString headerText = model()->headerData(logicalIndex, orientation()).toString();
- QString text = fontMetrics.elidedText(headerText, Qt::ElideRight, rect.width()-16);
- painterEx->setFont(font);
- bool isCheckableCol = (m_bCheckable && logicalIndex == 0);
- QRect textRect = rect.adjusted((isCheckableCol?44:0),0,0,0);
- if(!isCheckableCol && logicalIndex == 0)
- {
- textRect = rect.adjusted(12,0,0,0);
- }
- painterEx->DrawText(textRect, text, colorText, m_alignmentText);
-
- //底部横线
- painterEx->DrawBorder(rect, colorBorder, PainterEx::RectBorderBottom);
-
- //排序图标
- if(m_bSortable && !m_listSortDisable.contains(logicalIndex))
- {
- QColor upColor = (m_nCurSortedIndex == logicalIndex && m_stateCurSorted == Asc)?QColor(133,142,189,200):QColor(133,142,189,80);
- QColor downColor = (m_nCurSortedIndex == logicalIndex && m_stateCurSorted == Desc)?QColor(133,142,189,200):QColor(133,142,189,80);
- QSize textSize = fontMetrics.size(Qt::TextSingleLine, text);
- int xPos = textRect.left() + textSize.width() + 6;
- int yPos = rect.top() + (rect.height() - fontMetrics.capHeight())/2 + 1;
-
- painterEx->DrawTriangle(QRect(xPos, yPos, 10, 5), true, upColor);
- painterEx->DrawTriangle(QRect(xPos, yPos+7, 10, 5), false, downColor);
- }
- //checkbox图标
- if(m_bCheckable && logicalIndex == 0)
- {
- painterEx->save();
- QRect target(rect.left()+12, rect.top()+(rect.height()-16)/2, 16, 16);
- painterEx->DrawPixmap(target, GetCheckboxImage(m_checkState));
- painterEx->restore();
- }
- }
- void BaseHeader::OnSectionClicked(int logicalIndex)
- {
- if(m_bSortable && !m_listSortDisable.contains(logicalIndex))
- {
- if(m_nCurSortedIndex == logicalIndex)
- {
- int state = m_stateCurSorted;
- state = (state + 1)%3;
- m_stateCurSorted = SortState(state);
- emit sig_Sorted(logicalIndex, m_stateCurSorted);
- }
- else
- {
- m_nCurSortedIndex = logicalIndex;
- m_stateCurSorted = Asc;
- emit sig_Sorted(logicalIndex, Asc);
- }
- }
-
- if(m_bCheckable && logicalIndex == 0)
- {
- m_checkState = (m_checkState != Qt::Checked)?(Qt::Checked):(Qt::Unchecked);
- emit sig_Checked(m_checkState == Qt::Checked);
- }
- updateSection(logicalIndex);
- }
- QString BaseHeader::GetCheckboxImage(Qt::CheckState state) const
- {
- if(state == Qt::Unchecked)
- {
- return ":/checkbox_unchecked.png";
- }
- else if(state == Qt::PartiallyChecked)
- {
- return ":/checkbox_partcheked.png";
- }
- else if(state == Qt::Checked)
- {
- return ":/checkbox_checked.png";
- }
- return "";
- }
|