Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2647063
  • 博文数量: 416
  • 博客积分: 10220
  • 博客等级: 上将
  • 技术积分: 4193
  • 用 户 组: 普通用户
  • 注册时间: 2006-12-15 09:47
文章分类

全部博文(416)

文章存档

2022年(1)

2021年(1)

2020年(1)

2019年(5)

2018年(7)

2017年(6)

2016年(7)

2015年(11)

2014年(1)

2012年(5)

2011年(7)

2010年(35)

2009年(64)

2008年(48)

2007年(177)

2006年(40)

我的朋友

分类: C/C++

2009-05-21 22:35:16


#include
#include "window.h"
#include "cximage/ximage.h"
//int main(int argc, char *argv[])
//{
//    QApplication app(argc, argv);
//    Window window;
//    //window.setSourceModel(createMailModel(&window));
//    window.show();
//    return app.exec();
//}

#include    
#include    
#include    
#include

QRegion roundedRect(const QRect& rect, int r)
{
 QRegion region;
 // middle and borders
 region += rect.adjusted(r, 0, -r, 0);
 region += rect.adjusted(0, r, 0, -r);
 // top left
 QRect corner(rect.topLeft(), QSize(r*2, r*2));
 region += QRegion(corner, QRegion::Rectangle);
 // top right
 corner.moveTopRight(rect.topRight());
 region += QRegion(corner, QRegion::Rectangle);
 // bottom left
 corner.moveBottomLeft(rect.bottomLeft());
 region += QRegion(corner, QRegion::Rectangle);
 // bottom right
 corner.moveBottomRight(rect.bottomRight());
 region += QRegion(corner, QRegion::Rectangle);
 return region;
}


class ImTitleBar : public QWidget   
{  
 Q_OBJECT
public:
 ImTitleBar(QWidget *parent)  :QWidget(parent)
 {   
  frame = new QFrame(this);
  frame->resize(size());
  frame->setObjectName("TitleBar");
    
  setAutoFillBackground(false) ;  
  setAttribute(Qt::WA_TranslucentBackground);  
  // Use a brush with a Highlight color role to render the background
  setBackgroundRole(QPalette::Highlight) ;  
 }

protected:
 void paintEvent(QPaintEvent *)
 {
  frame->resize(size());
  QStyleOption opt;  
  opt.init(this);  
  QPainter p(this);  
  style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
 }
private:
 QFrame *frame;
 CxImage m_img;
};

class TitleBar : public QWidget   
{  
 Q_OBJECT
public: 
 TitleBar(QWidget *parent)
 {
  frame = new QFrame(this);
  frame->resize(size());
  frame->setObjectName("TitleBarBg");
  // Don't let this widget inherit the parent's backround color
  setAutoFillBackground(false) ;  
  setAttribute(Qt::WA_TranslucentBackground);  
  // Use a brush with a Highlight color role to render the background
  setBackgroundRole(QPalette::Highlight) ;  
  //setPalette(QPalette(QColor(250, 0, 0)));  
  minimize = new QToolButton(this) ;  
  maximize = new QToolButton(this) ;  
  close= new QToolButton(this) ;  
  setFixedHeight(35);  
  // Use the style to set the button pixmaps
  QPixmap pix = style()->standardPixmap(QStyle::SP_TitleBarCloseButton) ;  
  close->setIcon(pix) ;  
  close->setObjectName("Close");  
  maxPix = style()->standardPixmap(QStyle::SP_TitleBarMaxButton) ;  
  maximize->setIcon(maxPix) ;  
  pix = style()->standardPixmap(QStyle::SP_TitleBarMinButton) ;  
  minimize->setIcon(pix) ;  
  restorePix = style()->standardPixmap(QStyle::SP_TitleBarNormalButton) ;  

  minimize->setMinimumHeight(20) ;  
  close->setMinimumHeight(20) ;  
  maximize->setMinimumHeight(20) ;  

  QLabel *label = new QLabel(this) ;  
  label->setObjectName("title");  
  label->setText("Window Label") ;  
  parent->setWindowTitle("Window Title") ;  

  QHBoxLayout *hbox = new QHBoxLayout(this) ;  
  hbox->addWidget(label) ;  
  hbox->addWidget(minimize) ;  
  hbox->addWidget(maximize) ;  
  hbox->addWidget(close) ;  

  hbox->insertStretch(1, 500) ;  
  hbox->setSpacing(0) ;  
  setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed) ;  

  maxNormal = false ;  

  connect(close, SIGNAL( clicked() ), parent, SLOT(close() ) ) ;  
  connect(minimize, SIGNAL( clicked() ), this, SLOT(showSmall() ) ) ;  
  connect(maximize, SIGNAL( clicked() ), this, SLOT(showMaxRestore() ) ) ;  
 }
 public slots:
  void showSmall()
  {
   parentWidget()->showMinimized() ;  
  }
  void showMaxRestore()
  {
   if (maxNormal)
   {
    parentWidget()->showNormal() ;  
    maxNormal = !maxNormal ;  
    maximize->setIcon(maxPix) ;  
   } 
   else
   {
    parentWidget()->showMaximized() ;  
    maxNormal = !maxNormal ;  
    maximize->setIcon(restorePix) ;  
   }
  }
protected:  
 void mousePressEvent(QMouseEvent *me)
 {
  startPos = me->globalPos() ;  
  clickPos = mapToParent(me->pos()) ;  
 }
 void mouseMoveEvent(QMouseEvent *me)
 {
  if (maxNormal)
   return ;  
  parentWidget()->move(me->globalPos() - clickPos) ;  
 }
protected:
 void paintEvent(QPaintEvent *)
 {
  frame->resize(size());
  QStyleOption opt;  
  opt.init(this);  
  QPainter p(this);  
  style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
 }
private:
 QFrame *frame;
 QToolButton *minimize ;  
 QToolButton *maximize ;  
 QToolButton *close ;  
 QPixmap restorePix, maxPix ;  
 bool maxNormal ;  
 QPoint startPos ;  
 QPoint clickPos ;  
};

class Frame : public QFrame   
{
public:  
 Frame()
 {
  m_mouse_down = false ;  
  setFrameShape(Box) ;  
  setObjectName("titleFramexx");
  // Make this a borderless window which can't
  // be resized or moved via the window system
  setWindowFlags(Qt::FramelessWindowHint) ;  
  setMouseTracking(true) ;
  //setAttribute(Qt::WA_TranslucentBackground);

  m_titleBar = new TitleBar(this) ;  
  m_content = new QWidget(this) ;  
  QVBoxLayout *vbox = new QVBoxLayout(this) ;  
  vbox->addWidget(m_titleBar) ;  
  vbox->setMargin(0) ;  
  vbox->setSpacing(0) ;  

  QVBoxLayout *layout = new QVBoxLayout(this) ;  
  layout->addWidget(m_content) ;  
  layout->setMargin(5) ;  
  layout->setSpacing(0) ;  
  vbox->addLayout(layout) ;  
  setMask(roundedRect(rect(), 10));
 }
 // Allows you to access the content area of the frame  
 // where widgets and layouts can be added   
 QWidget *contentWidget() const
 {  
  return m_content ;  
 }
 TitleBar *titleBar() const
 {  
  return m_titleBar ;  
 }
 void mousePressEvent(QMouseEvent *e)
 {
  m_old_pos = e->pos() ;  
  m_mouse_down = e->button() == Qt::LeftButton ;  
 }

 void 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()) <= 5 ;  
   right = qAbs(x - r.right()) <= 5 ;  
   bottom = qAbs(y - r.bottom()) <= 5 ;  
   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 mouseReleaseEvent(QMouseEvent *e)
 {
  Q_UNUSED(e)  m_mouse_down = false ;  
 }
private:
 TitleBar *m_titleBar ;  
 QWidget *m_content ;  
 QPoint m_old_pos ;  
 bool m_mouse_down ;  
 bool left, right, bottom ;  

};  


#include "main.moc"
int main(int argc, char **argv)   

 Q_INIT_RESOURCE(styles);
 QApplication app(argc, argv) ;  

 Frame box ;  
 box.move(0,0) ;  

 QVBoxLayout *l = new QVBoxLayout(box.contentWidget()) ;  
 l->setMargin(0) ;  
 QTextEdit *edit = new QTextEdit(box.contentWidget()) ;  
 l->addWidget(edit) ;  
 l->addWidget(new QPushButton("Start")) ;  
 ImTitleBar *line = new ImTitleBar(NULL);
 //TitleBar *line = new TitleBar((QWidget *)&box);
 line->setFixedHeight(150);
 /*QLabel *label = new QLabel(line) ; 
 label->setText("Window Label") ;
 label->resize(200,150);*/
 l->addWidget(line) ;  

 box.show() ;  

 QString sStyle ="QPushButton{ background-color:red; } \
     TitleBar { color: red; } \
     TitleBar#title  { color: red;}\
     TitleBar::title { color: red;}\
     TitleBar QLabel { color: red;}\
     QFrame#titleFrame {border: 0; border-image: url(:/images/titlebar_c.png) 0 0 0 0;}\
     ImTitleBar QFrame#TitleBar {border-width: 30 30 5 30; border-image: url(:/images/titlebar_c.png) 30 30 5 30;}\
     TitleBar QFrame#TitleBarBg {border-width: 30 30 5 30; border-image: url(:/images/titlebar_c.png) 30 30 5 30;}\
     TitleBar QToolButton#Close{ background-color:red; }";
 app.setStyleSheet(sStyle);

 return app.exec() ;  
}

//void RoundedWidget::resizeEvent(QResizeEvent* event)
//{
// QPixmap pixmap(size());
// QPainter painter(&pixmap);
// painter.fillRect(pixmap.rect(), Qt::white);
// painter.setBrush(Qt::black);
// painter.drawRoundRect(pixmap.rect());
// setMask(pixmap.createMaskFromColor(Qt::white));
//}

阅读(2831) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2009-05-25 19:47:05

http://wiki.qtcentre.org/index.php?title=ARGB-Widgets