Blame view
UtteranceTreeWidget.cpp
9 KB
3f2992b2c
|
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 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 |
#include <QPainter> #include <QMouseEvent> #include <QDebug> #include "UtteranceTreeWidget.h" using namespace arma; UtteranceTreeWidget::UtteranceTreeWidget(int width, int height, QWidget *parent) : QWidget(parent), m_width(width), m_height(height), m_yPad(0.0), m_YRatio(1.0), m_posReleased(true) { setFixedSize(m_width, m_height); setMouseTracking(true); m_tree = new UtteranceTree; } QList<QList<int>> UtteranceTreeWidget::getPartition() { return m_tree->getPartition(); } /////////// // slots // /////////// void UtteranceTreeWidget::setDiff(const arma::mat &Diff) { m_tree->setDiff(Diff); m_tree->setTree(m_S, m_W, m_SigmaInv); m_cutDist = m_tree->getCutValues(); // m_tree->displayTree(m_map); update(); } void UtteranceTreeWidget::updateUtteranceTree(const mat &S, const mat &W, const umat &map, const mat &SigmaInv) { m_S = S; m_unnormS = S; m_W = W; m_SigmaInv = SigmaInv; m_map = map; m_tree->setTree(m_S, m_W, m_SigmaInv); m_cutDist = m_tree->getCutValues(); // m_tree->displayTree(m_map); update(); } void UtteranceTreeWidget::setDistance(UtteranceTree::DistType dist) { m_tree->setDist(dist); m_tree->setTree(m_S, m_W, m_SigmaInv); m_cutDist = m_tree->getCutValues(); // m_tree->displayTree(m_map); update(); } void UtteranceTreeWidget::setCovInv(const mat &CovInv) { m_SigmaInv = CovInv; m_tree->setTree(m_S, m_W, m_SigmaInv); m_cutDist = m_tree->getCutValues(); // m_tree->displayTree(m_map); update(); } void UtteranceTreeWidget::setAgrCrit(UtteranceTree::AgrCrit agr) { m_tree->setAgr(agr); m_tree->setTree(m_S, m_W, m_SigmaInv); m_cutDist = m_tree->getCutValues(); // m_tree->displayTree(m_map); update(); } void UtteranceTreeWidget::setWeight(const mat &W) { m_W = W; m_tree->setTree(m_S, m_W, m_SigmaInv); m_cutDist = m_tree->getCutValues(); // m_tree->displayTree(m_map); update(); } void UtteranceTreeWidget::setPartitionMethod(UtteranceTree::PartMeth partMeth) { m_tree->setPartMeth(partMeth); m_tree->setTree(m_S, m_W, m_SigmaInv); m_cutDist = m_tree->getCutValues(); // m_tree->displayTree(m_map); update(); } void UtteranceTreeWidget::normalizeVectors(bool checked) { UtteranceTree::DistType distType = m_tree->getDist(); qreal normFac; for (uword i(0); i < m_unnormS.n_rows; i++) { switch (distType) { case UtteranceTree::L2: normFac = as_scalar(m_unnormS.row(i) * m_unnormS.row(i).t()); break; case UtteranceTree::Mahal: normFac = as_scalar(m_unnormS.row(i) * m_SigmaInv * m_unnormS.row(i).t()); break; } if (checked) m_S.row(i) = m_unnormS.row(i) / normFac; else m_S.row(i) = m_unnormS.row(i); } m_tree->setTree(m_S, m_W, m_SigmaInv); m_cutDist = m_tree->getCutValues(); update(); } void UtteranceTreeWidget::paintEvent(QPaintEvent *event) { Q_UNUSED(event); QPainter painter(this); qreal x1, x2, y1, y2; // node coordinates qreal refWidth = 3.0 / 4.0 * m_width; // width available for drawing qreal refHeight = 3.0 / 4.0 * m_height; // height available for drawing qreal xPad = (m_width - refWidth) / 2; // shift along X-axis m_yPad = (m_height - refHeight) / 2; // shift along Y-axis int subRef; // subtitle index QString subLabel; // and its string version QFont font; int pixelSize(10); QString gradLabel; QList<QPoint> selectedNodes; qreal lineWidth(2.0); // font settings font.setBold(true); font.setPixelSize(pixelSize); painter.setFont(font); painter.setPen(QPen(QBrush(Qt::black), lineWidth)); // filling background painter.fillRect(QRect(0, 0, m_width, m_height), QBrush(Qt::white)); // retrieving nodes coordinates QList<QPair<QLineF, QPair<int, bool>>> coord; m_tree->getCoordinates(coord); // removing first line if (coord.size() > 0) coord.pop_front(); // number of instances qreal maxWidth(m_tree->getClusterSize()-1); // draw tree corresponding to possible partitions of the data set if (maxWidth > 0) { // horizontal and vertical scaling factors qreal XRatio(refWidth / maxWidth); qreal maxHeight(coord[0].first.y1()); m_YRatio = refHeight / maxHeight; // draw root if (coord[0].second.second) { x1 = xPad + coord[0].first.x1() * XRatio; y1 = m_height - (m_yPad + coord[0].first.y1() * m_YRatio); painter.drawLine(x1, 0, x1, y1); } // drawing y-axis x1 = 1; x2 = m_width - 1; y1 = m_yPad; y2 = m_height - m_yPad;; painter.drawLine(x1, y1, x1, y2); painter.drawLine(x2, y1, x2, y2); // drawing gradations painter.setPen(QPen(QBrush(Qt::black), lineWidth)); const int nGrad(3); const int gradWidth(4); for (int i(0); i <= nGrad; i++) { gradLabel = QString::number(i * maxHeight / nGrad, 'g', 3); x1 = 1; x2 = 1 + gradWidth; y1 = m_height - (m_yPad + i * refHeight / nGrad); painter.drawLine(x1, y1, x2, y1); painter.drawText(QRectF(x2 + 2, y1 - 7, xPad, m_yPad), Qt::AlignLeft, gradLabel); x1 = m_width - 1; x2 = m_width - (1 + gradWidth); painter.drawLine(x1, y1, x2, y1); painter.drawText(QRectF(x2 - (xPad + 2), y1 - 7, xPad, m_yPad), Qt::AlignRight, gradLabel); } // drawing tree(s) m_selectedUtter.clear(); QVector<QPair<qreal, qreal>> treeBound(m_cutDist.size()); for (int i(0); i < treeBound.size(); i++) treeBound[i] = QPair<qreal, qreal>(width(), 0.0); QList<QList<QPointF>> treesCoord; QList<QPointF> invisPoints; for (int i(0); i < coord.size(); i++) { // setting drawing color QColor color; bool visible = coord[i].second.second; color = Qt::black; // coordinates required to draw from parent node x1 = xPad + coord[i].first.x1() * XRatio; x2 = xPad + coord[i].first.x2() * XRatio; y1 = m_height - (m_yPad + coord[i].first.y1() * m_YRatio); y2 = m_height - (m_yPad + coord[i].first.y2() * m_YRatio); // adding root coordinates to dendogram lists of points if (coord[i].first.y1() == coord[0].first.y1()) { QList<QPointF> pointList; // case of single dendogram (unconstrained clustering) if (m_cutDist.size() == 1) { pointList << QPointF(x1, y1); if (treesCoord.isEmpty()) treesCoord.push_back(pointList); else treesCoord[0].push_back(QPointF(x2, y2)); } // case of multiple dendograms (constrained clustering) else if (y2 > y1) { pointList << QPointF(x2, y2); treesCoord.push_back(pointList); } } // updating list of points for each dendogram int j(0); while (j < treesCoord.size() && !treesCoord[j].contains(QPointF(x1, y1))) j++; if (j < treesCoord.size()) { treesCoord[j].push_back(QPointF(x2, y2)); // adjusting dendogram boundaries qreal lim = m_height - (m_yPad + m_cutDist[j] * m_YRatio); if (x2 < treeBound[j].first && y1 < lim && y2 > lim) treeBound[j].first = x2; if (x2 > treeBound[j].second && y1 < lim && y2 > lim) treeBound[j].second = x2 + lineWidth; } // node label subRef = coord[i].second.first; // change color if mouse points to current node if ((((m_mousePos.x() <= x1 && m_mousePos.x() >= x2) || (m_mousePos.x() >= x1 && m_mousePos.x() <= x2)) && (m_mousePos.y() > y1 && m_mousePos.y() <= y2)) || selectedNodes.contains(QPoint(x1, y1))) { color = Qt::red; selectedNodes.push_back(QPoint(x2, y2)); if (subRef != -1) m_selectedUtter.push_back(as_scalar(m_map.col(subRef))); } // drawing from previous node if (!visible) { invisPoints.push_back(QPointF(x2, y2)); color = Qt::white; } else if (invisPoints.contains(QPointF(x1, y1))) { painter.setPen(QPen(QBrush(color), lineWidth)); painter.drawLine(x1, y1 - gradWidth * 4, x1, y1); } painter.setPen(QPen(QBrush(color), lineWidth)); painter.drawLine(x1, y1, x2, y1); painter.drawLine(x2, y1, x2, y2); } // drawing optimal cut(s) painter.setPen(QPen(QBrush(Qt::black), lineWidth / 2, Qt::DotLine)); for (int i(0); i < m_cutDist.size(); i++) { y1 = m_height - (m_yPad + m_cutDist[i] * m_YRatio); painter.drawLine(treeBound[i].first - lineWidth * 3, y1, treeBound[i].second + lineWidth * 2, y1); } qSort(m_selectedUtter); emit(setSpeakers(m_selectedUtter)); } } void UtteranceTreeWidget::mousePressEvent(QMouseEvent * event) { Q_UNUSED(event); m_posReleased = !m_posReleased; } void UtteranceTreeWidget::mouseDoubleClickEvent(QMouseEvent * event) { Q_UNUSED(event); if (!m_selectedUtter.isEmpty()) emit playSubtitle(m_selectedUtter); } void UtteranceTreeWidget::mouseMoveEvent(QMouseEvent * event) { if (m_posReleased) { m_mousePos.setX(event->x()); m_mousePos.setY(event->y()); update(); } } void UtteranceTreeWidget::setCurrSubtitle(int subIdx) { QString currSub = QString::number(subIdx+1); if (currSub != m_currSub) { m_currSub = currSub; update(); } } void UtteranceTreeWidget::releasePos(bool released) { m_posReleased = released; } |