File indexing completed on 2024-05-19 05:06:00
0001 /******************************************************************************************** 0002 begin : Sunday Jun 27 2008 0003 copyright : (C) 2008 by Mathias Soeken (msoeken@informatik.uni-bremen.de) 0004 copyright : (C) 2005-2006 by Holger Danielsson (holger.danielsson@t-online.de) 0005 ********************************************************************************************/ 0006 0007 /*************************************************************************** 0008 * * 0009 * This program is free software; you can redistribute it and/or modify * 0010 * it under the terms of the GNU General Public License as published by * 0011 * the Free Software Foundation; either version 2 of the License, or * 0012 * (at your option) any later version. * 0013 * * 0014 ***************************************************************************/ 0015 0016 #include "selectframeaction.h" 0017 0018 #include <QFrame> 0019 #include <QHBoxLayout> 0020 #include <QPainter> 0021 #include <QPaintEvent> 0022 #include <QToolBar> 0023 #include <QToolButton> 0024 #include <QVBoxLayout> 0025 0026 #include <QAction> 0027 #include <QDialog> 0028 #include <QIcon> 0029 #include <KLocalizedString> 0030 #include <QMenu> 0031 #include <QPushButton> 0032 #include <KConfigGroup> 0033 0034 #include "tabularcell.h" 0035 0036 namespace KileDialog { 0037 0038 //BEGIN Icons for standard frames 0039 static const char* const all_border_xpm[] = { 0040 "14 14 2 1", 0041 "# c #000000", 0042 ". c #ffffff", 0043 "##############", 0044 "#............#", 0045 "#............#", 0046 "#............#", 0047 "#............#", 0048 "#............#", 0049 "#............#", 0050 "#............#", 0051 "#............#", 0052 "#............#", 0053 "#............#", 0054 "#............#", 0055 "#............#", 0056 "##############" 0057 }; 0058 0059 static const char* const lr_border_xpm[] = { 0060 "14 14 2 1", 0061 "# c #000000", 0062 ". c #ffffff", 0063 "#............#", 0064 "#............#", 0065 "#............#", 0066 "#............#", 0067 "#............#", 0068 "#............#", 0069 "#............#", 0070 "#............#", 0071 "#............#", 0072 "#............#", 0073 "#............#", 0074 "#............#", 0075 "#............#", 0076 "#............#" 0077 }; 0078 0079 static const char* const tb_border_xpm[] = { 0080 "14 14 2 1", 0081 "# c #000000", 0082 ". c #ffffff", 0083 "##############", 0084 "..............", 0085 "..............", 0086 "..............", 0087 "..............", 0088 "..............", 0089 "..............", 0090 "..............", 0091 "..............", 0092 "..............", 0093 "..............", 0094 "..............", 0095 "..............", 0096 "##############" 0097 }; 0098 0099 static const char* const no_border_xpm[] = { 0100 "14 14 2 1", 0101 "# c #000000", 0102 ". c #ffffff", 0103 "..............", 0104 "..............", 0105 "..............", 0106 "..............", 0107 "..............", 0108 "..............", 0109 "..............", 0110 "..............", 0111 "..............", 0112 "..............", 0113 "..............", 0114 "..............", 0115 "..............", 0116 ".............." 0117 }; 0118 //END 0119 0120 //BEGIN TabularFrameWidget 0121 class TabularFrameWidget : public QFrame 0122 { 0123 public: 0124 TabularFrameWidget(QWidget* parent = Q_NULLPTR); 0125 void setBorder(int value); 0126 int border() const { 0127 return m_border; 0128 } 0129 0130 protected: 0131 virtual void paintEvent(QPaintEvent *event) override; 0132 virtual void mousePressEvent(QMouseEvent *event) override; 0133 0134 private: 0135 int m_border; 0136 QRect m_left, m_top, m_right, m_bottom; 0137 }; 0138 0139 TabularFrameWidget::TabularFrameWidget(QWidget* parent) 0140 : QFrame(parent) 0141 { 0142 m_border = TabularCell::None; 0143 0144 QPalette p = palette(); 0145 p.setColor(backgroundRole(), QColor(Qt::white)); 0146 setPalette(p); 0147 setFixedWidth(120); 0148 setFixedHeight(120); 0149 setLineWidth(2); 0150 setFrameStyle(QFrame::Box | QFrame::Raised); 0151 0152 QRect r = contentsRect(); 0153 int x1 = r.left(); 0154 int y1 = r.top(); 0155 int x2 = r.right(); 0156 int y2 = r.bottom(); 0157 0158 m_left.setRect(x1, y1 + 20, 20, y2 - 43); 0159 m_top.setRect(x1 + 20, y1, x2 - 43, 20); 0160 m_right.setRect(x2 - 20, y1 + 20, 20, y2 - 43); 0161 m_bottom.setRect(x1 + 20, y2 - 20, x2 - 43, 20); 0162 } 0163 0164 void TabularFrameWidget::setBorder(int value) 0165 { 0166 m_border = value; 0167 update(); 0168 } 0169 0170 void TabularFrameWidget::paintEvent(QPaintEvent *event) 0171 { 0172 Q_UNUSED(event); 0173 QPainter painter(this); 0174 0175 QRect r = contentsRect(); 0176 int x1 = r.left(); 0177 int y1 = r.top(); 0178 int x2 = r.right(); 0179 int y2 = r.bottom(); 0180 0181 // left/top 0182 painter.setPen(Qt::black); 0183 painter.drawLine(x1 + 6, y1 + 14, x1 + 14, y1 + 14); 0184 painter.drawLine(x1 + 14, y1 + 14, x1 + 14, y1 + 6); 0185 0186 // left/bottom 0187 painter.drawLine(x1 + 6, y2 - 14, x1 + 14, y2 - 14); 0188 painter.drawLine(x1 + 14, y2 - 14, x1 + 14, y2 - 6); 0189 0190 // right/top 0191 painter.drawLine(x2 - 6, y1 + 14, x2 - 14, y1 + 14); 0192 painter.drawLine(x2 - 14, y1 + 14, x2 - 14, y1 + 6); 0193 0194 // right/bottom 0195 painter.drawLine(x2 - 6, y2 - 14, x2 - 14, y2 - 14); 0196 painter.drawLine(x2 - 14, y2 - 14, x2 - 14, y2 - 6); 0197 0198 // centered rectangle 0199 painter.setPen(Qt::gray); 0200 painter.setBrush(Qt::gray); 0201 painter.drawRect(x1 + 20, y1 + 20, x2 - 43, y2 - 43); 0202 0203 //QPen pen = QPen(Qt::red,4); 0204 QPen pen = QPen(Qt::black, 4); 0205 painter.setPen(pen); 0206 if(m_border & TabularCell::Left) { 0207 painter.drawLine(x1 + 10, y1 + 20, x1 + 10, y2 - 20); 0208 } 0209 if(m_border & TabularCell::Top) { 0210 painter.drawLine(x1 + 20, y1 + 10, x2 - 20, y1 + 10); 0211 } 0212 if(m_border & TabularCell::Right) { 0213 painter.drawLine(x2 - 10, y1 + 20, x2 - 10, y2 - 20); 0214 } 0215 if(m_border & TabularCell::Bottom) { 0216 painter.drawLine(x1 + 20, y2 - 10, x2 - 20, y2 - 10); 0217 } 0218 } 0219 0220 void TabularFrameWidget::mousePressEvent(QMouseEvent *event) 0221 { 0222 if (event->button() != Qt::LeftButton) 0223 return; 0224 0225 int x = event->x(); 0226 int y = event->y(); 0227 0228 int state = 0; 0229 if(m_left.contains(x, y)) 0230 state = TabularCell::Left; 0231 else if(m_top.contains(x, y)) 0232 state = TabularCell::Top; 0233 else if(m_right.contains(x, y)) 0234 state = TabularCell::Right; 0235 else if(m_bottom.contains(x, y)) 0236 state = TabularCell::Bottom; 0237 0238 if(state > 0) { 0239 if(m_border & state) { 0240 m_border &= ~state; 0241 } 0242 else { 0243 m_border |= state; 0244 } 0245 update(); 0246 } 0247 } 0248 //END 0249 0250 SelectFrameAction::SelectFrameAction(const QString &text, QToolBar *parent) 0251 : KToolBarPopupAction(QIcon(), text, parent), 0252 m_Parent(parent), 0253 m_CurrentBorder(TabularCell::None) 0254 { 0255 setIcon(generateIcon()); 0256 0257 QWidget *page = new QWidget(parent); 0258 QVBoxLayout *layout = new QVBoxLayout(); 0259 layout->setContentsMargins(0, 0, 0, 0); 0260 layout->setSpacing(0); 0261 page->setLayout(layout); 0262 0263 QWidget *buttonBox = new QWidget(page); 0264 QHBoxLayout *buttonBoxLayout = new QHBoxLayout(); 0265 buttonBoxLayout->setContentsMargins(0, 0, 0, 0); 0266 buttonBox->setLayout(buttonBoxLayout); 0267 0268 m_pbNone = new QToolButton(buttonBox); 0269 m_pbLeftRight = new QToolButton(buttonBox); 0270 m_pbTopBottom = new QToolButton(buttonBox); 0271 m_pbAll = new QToolButton(buttonBox); 0272 0273 m_pbNone->setIcon(QIcon(QPixmap(const_cast<const char**>(no_border_xpm)))); 0274 m_pbLeftRight->setIcon(QIcon(QPixmap(const_cast<const char**>(lr_border_xpm)))); 0275 m_pbTopBottom->setIcon(QIcon(QPixmap(const_cast<const char**>(tb_border_xpm)))); 0276 m_pbAll->setIcon(QIcon(QPixmap(const_cast<const char**>(all_border_xpm)))); 0277 0278 buttonBoxLayout->addStretch(); 0279 buttonBoxLayout->addWidget(m_pbNone); 0280 buttonBoxLayout->addWidget(m_pbLeftRight); 0281 buttonBoxLayout->addWidget(m_pbTopBottom); 0282 buttonBoxLayout->addWidget(m_pbAll); 0283 buttonBoxLayout->addStretch(); 0284 0285 QWidget *frameWidget = new QWidget(page); 0286 QHBoxLayout *frameWidgetLayout = new QHBoxLayout(); 0287 frameWidgetLayout->setContentsMargins(0, 0, 0, 0); 0288 frameWidget->setLayout(frameWidgetLayout); 0289 0290 m_FrameWidget = new TabularFrameWidget(frameWidget); 0291 0292 frameWidgetLayout->addStretch(); 0293 frameWidgetLayout->addWidget(m_FrameWidget); 0294 frameWidgetLayout->addStretch(); 0295 0296 m_pbDone = new QPushButton(QIcon::fromTheme("dialog-ok-apply"), i18n("Apply"), page); 0297 0298 layout->addWidget(buttonBox); 0299 layout->addWidget(frameWidget); 0300 layout->addWidget(m_pbDone); 0301 0302 QWidgetAction *widgetAction = new QWidgetAction(this); 0303 widgetAction->setDefaultWidget(page); 0304 menu()->addAction(widgetAction); 0305 0306 connect(this, SIGNAL(triggered(bool)), 0307 this, SLOT(slotTriggered())); 0308 connect(m_pbNone, SIGNAL(clicked()), 0309 this, SLOT(slotNoneClicked())); 0310 connect(m_pbLeftRight, SIGNAL(clicked()), 0311 this, SLOT(slotLeftRightClicked())); 0312 connect(m_pbTopBottom, SIGNAL(clicked()), 0313 this, SLOT(slotTopBottomClicked())); 0314 connect(m_pbAll, SIGNAL(clicked()), 0315 this, SLOT(slotAllClicked())); 0316 connect(m_pbDone, SIGNAL(clicked()), 0317 this, SLOT(slotDoneClicked())); 0318 } 0319 0320 QIcon SelectFrameAction::generateIcon() 0321 { 0322 QPixmap pixmap(m_Parent->iconSize()); 0323 0324 QPainter painter(&pixmap); 0325 painter.fillRect(pixmap.rect(), Qt::gray); 0326 0327 painter.setPen(Qt::black); 0328 if(m_CurrentBorder & TabularCell::Left) 0329 painter.drawLine(0, 0, 0, pixmap.height() - 1); 0330 if(m_CurrentBorder & TabularCell::Top) 0331 painter.drawLine(0, 0, pixmap.width() - 1, 0); 0332 if(m_CurrentBorder & TabularCell::Right) 0333 painter.drawLine(pixmap.width() - 1, 0, pixmap.width() - 1, pixmap.height() - 1); 0334 if(m_CurrentBorder & TabularCell::Bottom) 0335 painter.drawLine(0, pixmap.height() - 1, pixmap.width() - 1, pixmap.height() - 1); 0336 0337 painter.end(); 0338 0339 return QIcon(pixmap); 0340 } 0341 0342 void SelectFrameAction::slotTriggered() 0343 { 0344 emit borderSelected(m_CurrentBorder); 0345 } 0346 0347 void SelectFrameAction::slotNoneClicked() 0348 { 0349 m_FrameWidget->setBorder(TabularCell::None); 0350 } 0351 0352 void SelectFrameAction::slotLeftRightClicked() 0353 { 0354 m_FrameWidget->setBorder(TabularCell::Left | TabularCell::Right); 0355 } 0356 0357 void SelectFrameAction::slotTopBottomClicked() 0358 { 0359 m_FrameWidget->setBorder(TabularCell::Top | TabularCell::Bottom); 0360 } 0361 0362 void SelectFrameAction::slotAllClicked() 0363 { 0364 m_FrameWidget->setBorder(TabularCell::Left | TabularCell::Right | TabularCell::Top | TabularCell::Bottom); 0365 } 0366 0367 void SelectFrameAction::slotDoneClicked() 0368 { 0369 int newBorder = m_FrameWidget->border(); 0370 if(m_CurrentBorder != newBorder) { 0371 m_CurrentBorder = newBorder; 0372 setIcon(generateIcon()); 0373 } 0374 emit borderSelected(newBorder); 0375 menu()->hide(); 0376 } 0377 0378 }