#include "ImMainWindow.h"
#define BORDERWIDTH 5
ImMainWindow::ImMainWindow( QWidget *parent )
:QMainWindow(parent)
{
m_mouse_down = false ;
setObjectName("ImMainWindow");
setWindowFlags(Qt::FramelessWindowHint );
setAttribute(Qt::WA_TranslucentBackground);
setAttribute(Qt::WA_NoSystemBackground, true);
setMouseTracking(true) ;
m_titleBar = new ImTitleBar(this) ;
m_menuBar = new QMenuBar(this);
m_menuBar->setObjectName("mainMenuBar");
//setMenuBar(m_menuBar);
createActions();
createMenus();
m_toolBar = new QToolBar(tr("File"), this);
addToolBar( m_toolBar);
createToolBars();
m_content = new QTextEdit(this) ;
m_content->setObjectName("contentWidget");
m_content->resize(500, 200);
QVBoxLayout *inbox = new QVBoxLayout;
inbox->setMenuBar(m_menuBar);
inbox->addWidget(m_toolBar);
inbox->setMargin(0) ;
inbox->setSpacing(0) ;
inbox->setContentsMargins(0 ,0, 0, 0);
QWidget *intWgt = new QWidget(this);
intWgt->setWindowFlags(Qt::FramelessWindowHint );
intWgt->setAttribute(Qt::WA_TranslucentBackground);
intWgt->setAttribute(Qt::WA_NoSystemBackground, true);
intWgt->setLayout(inbox);
QVBoxLayout *vbox = new QVBoxLayout;
vbox->addWidget(intWgt);
vbox->setMargin(0) ;
vbox->setSpacing(0) ;
vbox->setContentsMargins(0 ,0, 0, 0);
m_frame = new QFrame(this);
m_frame->setObjectName("menuFrame");
m_frame->setWindowFlags(Qt::FramelessWindowHint );
m_frame->setAttribute(Qt::WA_TranslucentBackground);
m_frame->setAttribute(Qt::WA_NoSystemBackground, true);
m_frame->setMouseTracking(true) ;
m_frame->setLayout(vbox);
QVBoxLayout *layout = new QVBoxLayout(this);
layout->addWidget(m_frame);
layout->addWidget(m_content) ;
//layout->setMargin(5) ;
layout->setSpacing(0);
layout->setContentsMargins(0, 0, 0, 0);
setLayout(layout);
//setCentralWidget(m_content);
}
void ImMainWindow::setWindowTitleText(const QString title)
{
m_titleBar->setWindowTitleText(title);
}
// Allows you to access the content area of the frame
// where widgets and layouts can be added
QWidget *ImMainWindow::contentWidget() const
{
return m_content ;
}
ImTitleBar *ImMainWindow::titleBar() const
{
return m_titleBar ;
}
void ImMainWindow::mousePressEvent(QMouseEvent *e)
{
m_old_pos = e->pos() ;
m_mouse_down = e->button() == Qt::LeftButton ;
}
void ImMainWindow::mouseMoveEvent(QMouseEvent *e)
{
int x = e->x() ;
int y = e->y() ;
if (m_mouse_down)
{
int dx = x - m_old_pos.x() ;
int dy = y - m_old_pos.y() ;
QRect g = geometry() ;
if (left)
g.setLeft(g.left() + dx) ;
if (right)
g.setRight(g.right() + dx) ;
if (bottom)
g.setBottom(g.bottom() + dy) ;
setGeometry(g) ;
m_old_pos = QPoint(!left ? e->x() : m_old_pos.x(), e->y()) ;
}
else
{
QRect r = rect() ;
left = qAbs(x - r.left()) <= BORDERWIDTH ;
right = qAbs(x - r.right()) <= BORDERWIDTH ;
bottom = qAbs(y - r.bottom()) <= BORDERWIDTH ;
bool hor = left | right ;
if (hor && bottom)
{
if (left)
setCursor(Qt::SizeBDiagCursor) ;
else
setCursor(Qt::SizeFDiagCursor) ;
}
else if (hor)
{
setCursor(Qt::SizeHorCursor) ;
}
else if (bottom)
{
setCursor(Qt::SizeVerCursor) ;
}
else
{
setCursor(Qt::ArrowCursor) ;
}
}
}
void ImMainWindow::mouseReleaseEvent(QMouseEvent *e)
{
Q_UNUSED(e) m_mouse_down = false ;
}
void ImMainWindow::paintEvent( QPaintEvent* event )
{
Q_UNUSED(event);
QStyleOptionFrame opt;
opt.init(this);
QPainter p(this);
style()->drawPrimitive(QStyle::PE_Frame, &opt, &p, this);
}
void ImMainWindow::resizeEvent( QResizeEvent * event )
{
//QMainWindow::resizeEvent(event);
int height = m_titleBar->height();
m_titleBar->resize(size().width(), height);
m_frame->setGeometry(6, height, size().width()-30,
m_menuBar->height() + m_toolBar->height());
//m_menuBar->move(0, height);
m_content->move(6, height + m_frame->height() + 10);
}
//! [8]
void ImMainWindow::createMenus()
{
//! [9] //! [10]
fileMenu = m_menuBar->addMenu(tr("&File"));
fileMenu->addAction(newAct);
//! [9]
fileMenu->addAction(openAct);
editMenu = m_menuBar->addMenu(tr("&Edit"));
editMenu->addAction(newAct);
//! [9]
editMenu->addAction(openAct);
}
//! [4]
void ImMainWindow::createActions()
{
//! [5]
newAct = new QAction(tr("&New"), this);
newAct->setShortcuts(QKeySequence::New);
newAct->setStatusTip(tr("Create a new file"));
connect(newAct, SIGNAL(triggered()), this, SLOT(newFile()));
//! [4]
openAct = new QAction(tr("&Open..."), this);
openAct->setShortcuts(QKeySequence::Open);
openAct->setStatusTip(tr("Open an existing file"));
connect(openAct, SIGNAL(triggered()), this, SLOT(open()));
//! [5]
}
void ImMainWindow::createToolBars()
{
//m_toolBar = addToolBar(tr("File"));
m_toolBar->addAction(newAct);
//! [29] //! [31]
m_toolBar->addAction(openAct);
}
/********************************************
QTreeView的header上加Checkbox
1. 构造中加入
QCheckBox *pCheck = new QCheckBox(header());
pCheck->setObjectName("headerCheck");
pCheck->setCheckState(Qt::Checked);
connect(header(), SIGNAL(sectionResized(int, int, int)), this, SLOT(slotRefreshLastColumnKey(int, int, int)));
2,slotRefreshLastColumnKey(int, int, int)中处理位置
QCheckBox * checkbox = qFindChild(header(), "headerCheck");
if(checkbox)
{
if(!header()->isSectionHidden(0))
{
int left = header()->sectionViewportPosition(0);
checkbox->move(left + (header()->sectionSize(0) - checkbox->width()) / 2,
(header()->height() - checkbox->height()) / 2);
}
else
checkbox->move(-100,0);
}