| 1 | /* Copyright 2012 Kjetil S. Matheussen |
| 2 | |
| 3 | This program is free software; you can redistribute it and/or |
| 4 | modify it under the terms of the GNU General Public License |
| 5 | as published by the Free Software Foundation; either version 2 |
| 6 | of the License, or (at your option) any later version. |
| 7 | |
| 8 | This program is distributed in the hope that it will be useful, |
| 9 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | GNU General Public License for more details. |
| 12 | |
| 13 | You should have received a copy of the GNU General Public License |
| 14 | along with this program; if not, write to the Free Software |
| 15 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ |
| 16 | |
| 17 | #ifndef QT_MYQCHECKBOX_H |
| 18 | #define QT_MYQCHECKBOX_H |
| 19 | |
| 20 | #include <QCheckBox> |
| 21 | #include <QMouseEvent> |
| 22 | #include <QString> |
| 23 | |
| 24 | #include "../Qt/EditorWidget.h" |
| 25 | |
| 26 | #include "../audio/undo_audio_effect_proc.h" |
| 27 | #include "../common/undo_patchvoice_proc.h" |
| 28 | |
| 29 | #ifdef COMPILING_RADIUM |
| 30 | extern struct Root *root; |
| 31 | #else |
| 32 | extern QColor *g_colors; |
| 33 | #endif |
| 34 | |
| 35 | static QColor mix_colors(const QColor &c1, const QColor &c2, float how_much){ |
| 36 | |
| 37 | float a1 = how_much; |
| 38 | float a2 = 1.0f-a1; |
| 39 | |
| 40 | if(c1.red()==0 && c1.green()==0 && c1.blue()==0){ // some of the black lines doesn't look look very good. |
| 41 | int r = 74*a1 + c2.red()*a2; |
| 42 | int g = 74*a1 + c2.green()*a2; |
| 43 | int b = 74*a1 + c2.blue()*a2; |
| 44 | |
| 45 | return QColor(r,g,b); |
| 46 | }else{ |
| 47 | |
| 48 | int r = c1.red()*a1 + c2.red()*a2; |
| 49 | int g = c1.green()*a1 + c2.green()*a2; |
| 50 | int b = c1.blue()*a1 + c2.blue()*a2; |
| 51 | |
| 52 | return QColor(r,g,b); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | static int get_text_width(QString text){ |
| 57 | const QFontMetrics fn = QFontMetrics(QFont()); |
| 58 | return fn.width(text); |
| 59 | } |
| 60 | |
| 61 | inline static void CHECKBOX_paint(QPainter *painter, bool is_checked, bool is_enabled, int width, int height, QString text){ |
| 62 | #ifdef COMPILING_RADIUM |
| 63 | QColor *colors = static_cast<EditorWidget*>(root->song->tracker_windows->os_visual.widget)->colors; |
| 64 | #else |
| 65 | QColor *colors = g_colors; |
| 66 | #endif |
| 67 | |
| 68 | QColor col; // on |
| 69 | |
| 70 | if(text!=""){ |
| 71 | col = colors[13]; |
| 72 | col = mix_colors(col.light(70),QColor(98,59,33),0.55);//editor->colors[colnum].light(52); |
| 73 | col.setAlpha(76); |
| 74 | }else{ |
| 75 | col = colors[9].light(52); |
| 76 | } |
| 77 | |
| 78 | |
| 79 | if(is_enabled==false) |
| 80 | col.setAlpha(col.alpha()/3); |
| 81 | |
| 82 | //col = QColor(106, 104, 100, 255); |
| 83 | //col = QColor(0, 107, 156, 255); |
| 84 | |
| 85 | if(is_checked==true){ |
| 86 | painter->fillRect(1,1,width-2,height-1,col); |
| 87 | //painter->setPen(editor->colors[1]); |
| 88 | //p.drawRect(0,0,width()-1,height()-1); |
| 89 | //p.drawRect(1,1,width()-3,height()-3); |
| 90 | }else{ |
| 91 | painter->setPen(col); |
| 92 | painter->drawRect(1,1,width-3,height-2); |
| 93 | painter->drawRect(2,2,width-5,height-4); |
| 94 | } |
| 95 | |
| 96 | |
| 97 | if(text!=""){ |
| 98 | painter->setPen(colors[15]); |
| 99 | painter->drawRect(0,0,width,height); |
| 100 | |
| 101 | //QRect rect(5,3,width-5,height-3); |
| 102 | QRect rect(1,1,width-2,height-1);//5,3,width-5,height-3); |
| 103 | QColor black(0,0,0); |
| 104 | if(is_checked==true) |
| 105 | black.setAlpha(190); |
| 106 | else |
| 107 | black.setAlpha(120); |
| 108 | painter->setPen(black); |
| 109 | |
| 110 | if(text=="Loop") |
| 111 | text = text + " " + QChar(8634); |
| 112 | |
| 113 | if(height>width){ |
| 114 | painter->save(); |
| 115 | painter->translate(0,0); |
| 116 | painter->rotate(90); |
| 117 | int text_width = get_text_width(text); |
| 118 | int pos = (height-text_width)/2; |
| 119 | painter->drawText(pos,-5, text); |
| 120 | painter->restore(); |
| 121 | }else{ |
| 122 | painter->drawText(rect, Qt::AlignCenter, text); |
| 123 | } |
| 124 | |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | struct MyQCheckBox : public QCheckBox{ |
| 129 | bool _has_mouse; |
| 130 | struct Patch *_patch; |
| 131 | int _effect_num; |
| 132 | bool _undo_patchvoice; |
| 133 | |
| 134 | QString vertical_text; |
| 135 | |
| 136 | void init(){ |
| 137 | _has_mouse=false; |
| 138 | _patch = NULL; |
| 139 | _effect_num = 0; |
| 140 | _undo_patchvoice = false; |
| 141 | } |
| 142 | |
| 143 | MyQCheckBox ( QWidget * parent = 0 ) : QCheckBox(parent) {init();} |
| 144 | MyQCheckBox ( const QString & text, QWidget * parent = 0) : QCheckBox(text,parent) {init();} |
| 145 | |
| 146 | void mousePressEvent ( QMouseEvent * event ) |
| 147 | { |
| 148 | if (event->button() == Qt::LeftButton){ |
| 149 | //setSliderDown(true); |
| 150 | #ifdef COMPILING_RADIUM |
| 151 | if(_undo_patchvoice==true) |
| 152 | Undo_PatchVoice_CurrPos(_patch,_effect_num); |
| 153 | else if(_patch!=NULL) |
| 154 | Undo_AudioEffect_CurrPos(_patch, _effect_num); |
| 155 | #endif |
| 156 | //handle_mouse_event(event); |
| 157 | _has_mouse = true; |
| 158 | printf("Got it %p %d. Checked: %d\n",_patch,_effect_num,!isChecked()); |
| 159 | setChecked(!isChecked()); |
| 160 | }else |
| 161 | QCheckBox::mousePressEvent(event); |
| 162 | } |
| 163 | |
| 164 | |
| 165 | void paintEvent ( QPaintEvent * ev ){ |
| 166 | QPainter p(this); |
| 167 | |
| 168 | if(text().startsWith("V ")){ |
| 169 | vertical_text = text().right(text().size()-2); |
| 170 | setText(""); |
| 171 | } |
| 172 | |
| 173 | CHECKBOX_paint(&p, isChecked(), isEnabled(), width(), height(), vertical_text!="" ? vertical_text : text()); |
| 174 | } |
| 175 | }; |
| 176 | |
| 177 | |
| 178 | #endif // QT_MYQCHECKBOX_H |