|
@@ -0,0 +1,146 @@
|
|
|
|
+#include "baseheader.h"
|
|
|
|
+#include "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 colorBG = QColor(255, 255, 255);
|
|
|
|
+ QColor colorBorder = QColor(255, 255, 255, 38);
|
|
|
|
+ // QColor colorText = QColor(210, 210, 210);
|
|
|
|
+ QColor colorText = QColor("#3A3F63");
|
|
|
|
+
|
|
|
|
+ //背景
|
|
|
|
+ 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, 20, 20);
|
|
|
|
+ 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 ":/icon/unchecked.png";
|
|
|
|
+ }
|
|
|
|
+ else if(state == Qt::PartiallyChecked)
|
|
|
|
+ {
|
|
|
|
+ return ":/icon/partially_checked.png";
|
|
|
|
+ }
|
|
|
|
+ else if(state == Qt::Checked)
|
|
|
|
+ {
|
|
|
|
+ return ":/icon/checked.png";
|
|
|
|
+ }
|
|
|
|
+ return "";
|
|
|
|
+}
|