1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
/** * ================================================================ * Enterprise Managerial * ================================================================ * * Project Info: http://portus.sourceforge.net * Project Lead: Francesco Portus (portusgraphics@gmail.com); * * (C) Copyright 2005, by Francesco Portus * * @author Francesco Portus * @version 0.1 * * This library is free software; you can redistribute it and/or modify it underthe terms * of the GNU Lesser General Public License as published by the Free Software Foundation; * either version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * See the GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License along with this * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, * Boston, MA 02111-1307, USA. */ package it.portus.view; import java.util.ArrayList; import java.util.Stack; import java.util.Vector; import com.trolltech.qt.core.QChildEvent; import com.trolltech.qt.core.QCoreApplication; import com.trolltech.qt.core.QEvent; import com.trolltech.qt.core.QObject; import com.trolltech.qt.core.QPoint; import com.trolltech.qt.core.QRect; import com.trolltech.qt.core.QSize; import com.trolltech.qt.core.Qt; import com.trolltech.qt.gui.QApplication; import com.trolltech.qt.gui.QColor; import com.trolltech.qt.gui.QGridLayout; import com.trolltech.qt.gui.QImage; import com.trolltech.qt.gui.QLineEdit; import com.trolltech.qt.gui.QListWidget; import com.trolltech.qt.gui.QPaintEvent; import com.trolltech.qt.gui.QPainter; import com.trolltech.qt.gui.QPushButton; import com.trolltech.qt.gui.QRegion; import com.trolltech.qt.gui.QVBoxLayout; import com.trolltech.qt.gui.QWidget; public class WidgetMirror extends QWidget { static final int MIRROR_HEIGHT = 38; private QWidget m_widget; private boolean m_inPaintEvent = false; public WidgetMirror(QWidget parent) { super(parent); m_widget = this; m_inPaintEvent = false; } void setMirroredWidget(QWidget w) { if (m_widget != null) { m_widget.removeEventFilter(this); Stack<QObject> parentStack = new Stack<QObject>(); parentStack.push(m_widget); while (!parentStack.isEmpty()) { QObject parent = parentStack.pop(); for (QObject child : parent.children()) { child.removeEventFilter(this); parentStack.push(child); } } } m_widget = w; if (m_widget != null) { m_widget.installEventFilter(this); setSizePolicy(m_widget.sizePolicy()); Stack<QObject> parentStack = new Stack<QObject>(); parentStack.push(m_widget); while (!parentStack.isEmpty()) { QObject parent = parentStack.pop(); for (QObject child : parent.children()) { if (child.isWidgetType()) { child.installEventFilter(this); parentStack.push(child); } } } } } public boolean eventFilter(QObject obj, QEvent event) { if (event.type() == QEvent.Type.ChildPolished) { QChildEvent e = (QChildEvent)event; QObject child = e.child(); if (child.isWidgetType()) { child.installEventFilter(this); Stack<QObject> parentStack = new Stack<QObject>(); parentStack.push(child); while (!parentStack.isEmpty()) { QObject parent = parentStack.pop(); for (QObject c : parent.children()) { if (c.isWidgetType()) { c.installEventFilter(this); parentStack.push(c); } } } } } else if (!m_inPaintEvent && obj.isWidgetType() && event.type() == QEvent.Type.Paint) { QWidget w = (QWidget)obj; QPaintEvent pe = (QPaintEvent)event; QRect r = new QRect(pe.rect().topLeft(), pe.rect().bottomRight()); final QPoint diff = w.mapTo(m_widget, new QPoint()); r.translate(diff); int tmp = r.top(); r.setTop(m_widget.height() - r.bottom() - 1); r.setBottom(m_widget.height() - tmp - 1); update(r); } return super.eventFilter(obj, event); } protected void paintEvent(QPaintEvent e) { if (m_widget == null) { return; } QRect dirty = new QRect(e.rect().topLeft(), e.rect().bottomRight()); dirty.setHeight(Math.min(dirty.height(), MIRROR_HEIGHT)); int tmp = dirty.top(); dirty.setTop(m_widget.height() - dirty.bottom() - 1); dirty.setBottom(m_widget.height() - tmp - 1); m_inPaintEvent = true; QImage im = new QImage(e.rect().size(), QImage.Format.Format_ARGB32_Premultiplied); QRegion region = new QRegion(dirty); RenderFlags flags = new QWidget.RenderFlags(QWidget.RenderFlag.resolve(QWidget.RenderFlag.DrawWindowBackground.value() | QWidget.RenderFlag.DrawChildren.value())); m_widget.render(im, dirty.topLeft().multiply(-1), region, flags);// | QWidget::IgnoreMask); im = im.mirrored(); QImage alphaChannel = new QImage(im.size(), QImage.Format.Format_Indexed8); Vector<Integer> s_colorTable = new Vector<Integer>(); if (s_colorTable.isEmpty()) { //s_colorTable.setSize(256); for (int i = 0; i < 256; ++i) { System.out.println(i); s_colorTable.add(i, QColor.fromRgb(i, i, i).rgb()); } } if (alphaChannel != null) { System.out.println("colors"); } alphaChannel.setColorTable(s_colorTable); alphaChannel.fill(0); final int maxAlpha = 100; for (int line = 0; line < alphaChannel.height() && line < MIRROR_HEIGHT - e.rect().top(); ++line) { final int x = line + e.rect().top(); int value = 4 * (maxAlpha - x * maxAlpha / MIRROR_HEIGHT) / (x + 4); // ; // std::memset(alphaChannel.scanLine(line), value, alphaChannel.bytesPerLine()); } im.setAlphaChannel(alphaChannel); QPainter p = new QPainter(this); p.drawImage(e.rect(), im); m_inPaintEvent = false; } public QSize sizeHint(){ if (m_widget == null) { return new QSize(); } QSize s = new QSize(m_widget.sizeHint().width(), m_widget.sizeHint().height()); s.setHeight(Math.min(s.height(), MIRROR_HEIGHT)); return s; } public static void main(String args[]) { QApplication.initialize(args); QCoreApplication.processEvents(); QWidget topLevel = new QWidget(); QWidget w = new QWidget(topLevel); WidgetMirror leMirror = new WidgetMirror(topLevel); leMirror.setMirroredWidget(w); QPushButton b = new QPushButton("Button", w); QLineEdit le = new QLineEdit(w); QListWidget lw = new QListWidget(w); lw.setMaximumHeight(64); ArrayList<String> items = new ArrayList<String>(); items.add("Item 1"); items.add("Item 2"); items.add("Item 3"); items.add("Item 4"); items.add("Item 5"); items.add("Item 6"); items.add("Item 7"); items.add("Item 8"); lw.addItems(items); QGridLayout layout = new QGridLayout(w); layout.setMargin(0); layout.addWidget(b, 0, 0, Qt.AlignmentFlag.AlignBottom); layout.addWidget(le, 0, 1, Qt.AlignmentFlag.AlignBottom); layout.addWidget(lw, 0, 2, Qt.AlignmentFlag.AlignBottom); QVBoxLayout outerLayout = new QVBoxLayout(topLevel); outerLayout.setSpacing(0); outerLayout.addWidget(w); outerLayout.addWidget(leMirror); topLevel.show(); QApplication.exec(); } }
Refactorings
No refactoring yet !
Hi at all, I try to reply a very cool widget maked by Matthias Kretz (http://vir.homelinux.org/blog/index.php?/archives/88-QWidgetrender.html), but it seem not work.
I believe that error is in a call at memset of alphaChannel.
Thanks a lot, predatelly.
P.S. Excuse for my English! :-)