logplus/logPlus/LineStyleView.cpp
2026-06-24 15:33:12 +08:00

180 lines
4.7 KiB
C++

#include "LineStyleView.h"
#include <QAbstractTableModel>
#include <QScrollBar>
#include <QHeaderView>
#include <QPainter>
#include <QPaintEvent>
#include <QDebug>
#include <QStyledItemDelegate>
class LineStyleModel:public QAbstractTableModel
{
public:
explicit LineStyleModel(QObject *parent=NULL):
QAbstractTableModel(parent){
m_PenList
<<(int) Qt::SolidLine
<<(int)Qt::DashLine
<<(int) Qt::DotLine
<<(int)Qt::DashDotLine
<<(int) Qt::DashDotDotLine;
}
~LineStyleModel(){}
// QAbstractItemModel interface
public:
virtual int rowCount(const QModelIndex &parent) const
{
return 5;
}
virtual int columnCount(const QModelIndex &parent) const
{
return 3;
}
virtual QVariant data(const QModelIndex &index, int role) const
{
if(!index.isValid() || index.row() <0 ||index.column() <0)
{
return QVariant();
}
if(role == Qt::DisplayRole)
{
return m_PenList.at(index.row());
}
return QVariant();
}
private:
QList<int> m_PenList;
};
class LineItemStyleDelegate:public QStyledItemDelegate
{
public:
LineItemStyleDelegate(QObject *parent=NULL):QStyledItemDelegate(parent) {
}
~LineItemStyleDelegate(){}
// QAbstractItemDelegate interface
public:
virtual void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
// QStyledItemDelegate::paint(painter,option,index);
QStyleOptionViewItemV3 v3=option;
QWidget *widget= (QWidget*)v3.widget;
QTableView* view=qobject_cast<QTableView*>(widget);
int colIndex=index.column();
QPen linePen;
QPointF p1,p2,pCenter;
QRect cellRect=option.rect;
Qt::PenStyle pStyle= (Qt::PenStyle)index.data(Qt::DisplayRole).value<int>();
if(view)
{
QModelIndex curIndex=view->currentIndex();
if(curIndex == index)
{
painter->save();
painter->setPen(Qt::NoPen);
painter->setBrush(Qt::lightGray);
painter->drawRect(cellRect);
painter->restore();
}
}
//if(pStyle == Qt::NoPen)
//{
// painter->drawText(cellRect,QString::fromLocal8Bit("无"),QTextOption(Qt::AlignCenter|Qt::AlignVCenter));
//}
linePen=painter->pen();
linePen.setWidthF(colIndex+1);
linePen.setStyle(pStyle);
//if(pStyle != Qt::NoPen)
painter->setPen(linePen);
pCenter=cellRect.center();
cellRect= cellRect.adjusted(10,0,-10,0);
p1.setX(cellRect.left());
p1.setY(pCenter.y());
p2.setX(cellRect.right());
p2.setY(pCenter.y());
//if(pStyle != Qt::NoPen)
painter->drawLine(p1,p2);
}
};
LineStyleView::LineStyleView(QWidget *parent)
: QTableView(parent),m_currentStyle((int)Qt::SolidLine),m_currentLineWidth(1)
{
horizontalHeader()->setVisible(false);
verticalHeader()->setVisible(false);
horizontalScrollBar()->setVisible(false);
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setGridStyle(Qt::NoPen);
setSelectionMode(QAbstractItemView::SingleSelection);
setSelectionBehavior(QAbstractItemView::SelectItems);
setModel(new LineStyleModel(this));
LineItemStyleDelegate *lineStyle=new LineItemStyleDelegate(this);
setMouseTracking(true);
setItemDelegate(lineStyle);
connect(this,SIGNAL(pressed(const QModelIndex&)),this,SLOT(slotCellPressed(const QModelIndex&)));
}
LineStyleView::~LineStyleView()
{
}
void LineStyleView::slotCellPressed(const QModelIndex &mIndex)
{
int row=mIndex.row();
int col=mIndex.column();
if (row <0 || col <0 )
{
return;
}
LineStyleModel* styleModel=dynamic_cast<LineStyleModel*>(model());
if (!styleModel)
{
return;
}
int styleValue=styleModel->data(mIndex,Qt::DisplayRole).toInt();
int lineWindth=mIndex.column()+1;
if (m_currentStyle != styleValue || m_currentLineWidth !=lineWindth)
{
m_currentStyle=styleValue;
m_currentLineWidth=lineWindth;
emit signalSelectLineStyleChanged(styleValue,lineWindth);
}
}
void LineStyleView::resizeEvent(QResizeEvent *reSize)
{
if(reSize->size().isValid())
{
int h=reSize->size().height();
int w=reSize->size().width();
int count= model()->columnCount();
int sValue=w/(float)count;
for(int j=0;j<count;j++)
{
setColumnWidth(j,sValue);
}
count= model()->rowCount();
sValue=h/(float)count;
for(int i=0;i<count;i++)
{
setRowHeight(i,sValue);
}
}
}