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 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511
|
/**
* @author Mark Larkin, Conway Institute, UCD. mark.larkin@ucd.ie
*
* Changes:
*
* 20-02-07,Nigel Brown(EMBL): made getExtraRowNum() a const member so it can
* be called inside sizeHint(); added eventFilter() to catch containing
* viewport resize events; added single setMaxNameLength() member to compute
* maximum typeset text width; added computeSize() member to determine final
* widget size from text size or containing viewport size; added
* horizontalMargin member. Visual effect of all this is to extend widget out
* to boundaries of viewport, allowing justification, colouring of whole
* region, and highlighting out to full width of viewport.
*
* 12-03-07,Nigel Brown(EMBL): changed background colour to very pale grey.
*
* 11-04-07,Nigel Brown(EMBL): Modified mousePressEvent(), keyPressEvent() and
* keyReleaseEvent() to extended sequence selection to allow [shift]-select
* for a block of sequences and [ctrl]-select to deselect a single sequence
* therein. Added new member 'ctrlPressed'. Initialised 'ctrlPressed' and
* 'rowSelected' in constructor. Note: on the Mac [ctrl] is achieved using the
* "Option" key (handled transparently by Qt). Behaviour on left-click is:
* select: select this row.
* [shift]-select: set block from last selected row to this row.
* [ctrl]-select: deselect this row.
* The member 'rowSelected' stores the last selected row. It is reset to an
* invalid value on [ctrl]-select.
*
* 12-4-07, Mark Larkin, Removed destructor. No need to delete QObjects.
*
* 25-04-07,Nigel Brown(EMBL): removed 'shiftPressed', 'ctrlPressed' members
* and the keyPressEvent() and keyReleaseEvent() handlers. These are now
* handled by the external KeyController class.
*
* 02-05-07,Nigel Brown(EMBL): Changed mousePressEvent() behaviour to add:
* [ctrl]-select: *toggle* this row.
*
* 09-05-07,Nigel Brown(EMBL): Added drag and [shift]-drag selection
* of row indentifiers via new member 'mousePressed', mouseMoveEvent()
* and mouseReleaseEvent(); Changed 'rowSelected' to 'firstRow' and
* added 'secondRow' store row range information.
*
* 09-05-07,Nigel Brown(EMBL): Added ensureVisible() call on new
* 'scrollArea' member inside mouseMoveEvent() to scroll when
* selection extends beyond visible.
*
* 15-05-07,Nigel Brown(EMBL): mousePressEvent(), mouseMoveEvent() now test
* numSequencesSoFar before handling event.
*
* 27-06-07,Nigel Brown(EMBL): For screen layout balancing: renamed
* numSequencesSoFar as numSequences, added numSequences2, which records the
* number of sequences in the *other* profile window; Added setNumSequences2()
* and changed computeSize() to compute height based on the *larger* of
* numSequences and numSequences2.
*
****************************************************************************/
#include <QtWidgets>
#include "SeqNameWidget.h"
#include "KeyController.h" //- nige
#include "clustalW/alignment/Alignment.h"
const int DEFAULT_MAX_NAME_LENGTH = 15;
const QColor DEFAULT_BACKGROUND_COLOUR(0xF9,0xF9,0xF9);
/**
* This is the constructor for a SeqNameWidget. It sets up all the initial parameters.
* @param num The number of sequence names to be displayed.
* @param boxsize The size of the box to contain the names.
* @param *parent The parent widget. This is optional.
*/
SeqNameWidget::SeqNameWidget(int boxsize, QFont* font, QWidget *parent)
: QWidget(parent),
firstSeq(0),
lastSeq(0),
nHead(0),
boxSize(boxsize)
{
/*
* Set up initial values
*/
isSet = false;
showNames = false;
mousePressed = false; //- nige
setMouseTracking(true);
offSetFromTop = 2; //it is 2 in AlignmentWidget
horizontalMargin = 5; //- nige
firstRow = -1; //- nige
secondRow = -1; //- nige
alignment = 0;
secStructName = "";
numSequences = 0;
numSequences2 = 0; //nige
viewSize = QSize(0,0);
/*
* Set the font
*/
displayFont = *font;
setMaxNameLength();
/*
* Set up the colors to be used in displaying the alignment
*/
selectedColor = new QColor;
selectedColor->setRgb(180,228,254);
backGroundGrey = new QColor;
backGroundGrey->setRgb(221,221,221);
/*
* Set the focus policy to allow the widget to recieve keyboard events
*/
setFocusPolicy( Qt::StrongFocus );
}
void SeqNameWidget::setNames(clustalw::Alignment* align)
{
alignment = align;
isSet = true;
if(align != 0)
{
numSequences = align->getNumSeqs();
selectedRows.clear();
}
selectedRows.resize(numSequences);
updateSizeHint(); //- may not be necessary now, 20-02-07,nige
}
void SeqNameWidget::updateDisplay(int fSeq, int lSeq, int nhead, QString ssName)
{
if(fSeq != 0 && lSeq != 0)
{
isSet = true;
showNames = true;
nHead = nhead;
secStructName = ssName;
firstSeq = fSeq;
lastSeq = lSeq;
numSequences = lastSeq - firstSeq + 1;
selectedRows.clear();
selectedRows.resize(numSequences);
setMaxNameLength(); //recompute width
}
else
{
isSet = false;
showNames = false;
nHead = 0;
secStructName = "";
firstSeq = fSeq;
lastSeq = lSeq;
numSequences = 0;
selectedRows.clear();
selectedRows.resize(numSequences);
updateSizeHint(); //- may not be necessary now, 20-02-07,nige
}
resize(computeSize());
update();
}
QSize SeqNameWidget::computeSize() const
{
if (! alignment)
return viewSize;
int extraRow = getExtraRowNum();
int width = DEFAULT_MAX_NAME_LENGTH;
int maxNumSeqs = (numSequences2 > numSequences ? numSequences2 : numSequences);
int height = (maxNumSeqs + extraRow) * boxSize;
if (maxNameLength > width) {
width = maxNameLength;
}
width += horizontalMargin * 2;
if (width < viewSize.width())
width = viewSize.width();
if (height < viewSize.height())
height = viewSize.height();
//printf("SNW::computeSize(%0x, w=%d, h=%d) [mnl=%d; bh=%d] [ns=%d:%d/%d]\n",
// (int)this, width, height, maxNameLength, boxSize, maxNumSeqs, numSequences, numSequences2);
return QSize(width, height);
}
/**
* This function overrides the parents sizeHint function. It gives an idea of
* the size this widget needs.
*/
//- may not be necessary now, 20-02-07,nige
QSize SeqNameWidget::sizeHint() const
{
QSize newSize = computeSize();
//printf("SeqNameWidget::sizeHint(%0x, w=%d, h=%d) [mnl=%d; bh=%d]\n",
// (int)this, newSize.width(), newSize.height(), maxNameLength, boxSize);
return newSize;
}
//- may not be necessary now, 20-02-07,nige
void SeqNameWidget::updateSizeHint()
{
QSize newSize = computeSize();
//printf("SeqNameWidget::updateSizeHint(%0x, w=%d, h=%d) [mnl=%d; bh=%d]\n",
// (int)this, newSize.width(), newSize.height(), maxNameLength, boxSize);
resize(newSize);
}
/**
* This function is an event handler. It finds the sequence name that has been clicked.
* @param *event A mouse event.
*
* 11-04-07,nige: extended to add [shift]-select and [ctrl]-select.
*/
void SeqNameWidget::mousePressEvent(QMouseEvent *event)
{
if (numSequences > 0 && event->button() == Qt::LeftButton) {
mousePressed = true;
KeyController *kbd = KeyController::Instance();
/* find which row has been selected */
int newRow = (event->y() / boxSize);
newRow -= getExtraRowNum();
/* starting over? */
if (firstRow < 0)
firstRow = secondRow = newRow;
if (0 <= newRow && newRow < (int)selectedRows.size()) {
if (kbd->shiftPressed()) {
/* select a range of rows */
if (firstRow < newRow) {
for (int i = firstRow; i <= newRow; i++) {
selectedRows[i] = 1;
}
} else {
for (int i = firstRow; i >= newRow; i--) {
selectedRows[i] = 1;
}
}
secondRow = newRow;
} else if (kbd->ctrlPressed()) {
/* nige: toggle */
if (selectedRows[newRow] == 0) {
/* select this row and leave firstRow */
selectedRows[newRow] = 1;
} else {
/* deselect this row and clear firstRow */
selectedRows[newRow] = 0;
firstRow = -1;
}
} else {
/* deselect previous selection and select this row */
for (int i = 0; i < numSequences; i++) {
selectedRows[i] = 0;
}
selectedRows[newRow] = 1;
firstRow = newRow;
}
}
update();
return;
}
QWidget::mousePressEvent(event);
}
void SeqNameWidget::mouseReleaseEvent(QMouseEvent *event) //nige
{
if (event->button() == Qt::LeftButton) {
mousePressed = false;
update();
return;
}
QWidget::mouseReleaseEvent(event);
}
void SeqNameWidget::mouseMoveEvent(QMouseEvent *event) //nige
{
KeyController *kbd = KeyController::Instance();
if (numSequences > 0 && mousePressed && !kbd->ctrlPressed()) {
//scroll if necessary to continue selection
scrollArea->ensureVisible(event->x(), event->y());
//which row? don't overshoot either end
int newRow = (event->y() / boxSize) - getExtraRowNum();
if (newRow < 0)
newRow = 0;
if (newRow >= numSequences)
newRow = numSequences - 1;
//printf("%d -> %d, %d\n", newRow, firstRow, secondRow);
if (firstRow <= secondRow) {
if (newRow < secondRow) {
//deselect between newRow and secondRow:
// F === N === S -> F === N --- S
for (int i = newRow+1; i <= secondRow; i++) {
//cout << "case 1\n";
selectedRows[i] = 0;
}
} else {
//reselect from firstRow to newRow
// F === S --- N -> F === S === N
for (int i = firstRow; i <= newRow; i++) {
//cout << "case 2\n";
selectedRows[i] = 1;
}
}
} else {
if (secondRow < newRow) {
//deselect between newRow and secondRow:
// S === N === F -> S --- N === F
for (int i = secondRow; i < newRow; i++) {
//cout << "case 3\n";
selectedRows[i] = 0;
}
} else {
//reselect from firstRow to newRow
// N --- S === F -> N === S === F
for (int i = newRow; i <= firstRow; i++) {
//cout << "case 4\n";
selectedRows[i] = 1;
}
}
}
secondRow = newRow;
update();
return;
}
QWidget::mouseMoveEvent(event);
}
/* 16-02-07,nige: track the containing viewport's size, then pass event on */
bool SeqNameWidget::eventFilter(QObject *o, QEvent *e)
{
//printf("SeqNameWidget::eventFilter(%0x, %d)\n", (int)this, e->type());
if (parentWidget() && parentWidget() == o) {
if (e->type() == QEvent::Resize) {
QResizeEvent *resizeEvent = static_cast<QResizeEvent*>(e);
//record known viewport size
viewSize = resizeEvent->size();
QSize newSize = computeSize();
resize(newSize);
//printf("SeqNameWidget::eventFilter(%0x, resize) nw=%d nh=%d, mnl=%d\n",
// (int)this, newSize.width(), newSize.height(), maxNameLength);
//fall through to allow the viewport to respond
return false;
}
}
return false;
}
/**
* This is an event handler. It repaints the widget when it needs to be repainted.
* @param *event A paint event.
*/
void SeqNameWidget::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
painter.fillRect(event->rect(), QBrush(DEFAULT_BACKGROUND_COLOUR));
painter.setFont(displayFont);
QRect redrawRect = event->rect();
// Set up the begin and end rows.
int extra = getExtraRowNum();
int beginRow = 0;
int endRow = numSequences - 1 + extra;
int currentRow = firstSeq;
//width of text drawing area
int textWidth = width() - horizontalMargin * 2;
QFontMetrics fontMetrics(displayFont);
painter.setPen(QPen(Qt::black));
//printf("SeqNameWidget::paintEvent(%0x, w=%d, h=%d) [mnl=%d; bh=%d]\n",
// (int)this, width(), height(), maxNameLength, boxSize);
if (firstSeq != 0 && isSet && alignment && showNames)
{
for (int row = beginRow; row <= endRow; ++row)
{
currentRow = firstSeq + row - extra;
if (row == 0)
{
painter.fillRect(0, 0, width(), boxSize,
QBrush(backGroundGrey->rgb()));
}
else if (nHead == 1 && row == 1)
{
painter.fillRect(0, row * boxSize, width(), boxSize,
QBrush(backGroundGrey->rgb()));
painter.drawText(QRect(horizontalMargin, row * boxSize + offSetFromTop, textWidth, boxSize),
Qt::AlignRight, secStructName);
}
else
{
// If the row has been selected, then highlight it.
if ((int)selectedRows.size() > row - extra && selectedRows[row - extra] == 1)
{
painter.fillRect(0, row * boxSize, width(), boxSize,
QBrush(selectedColor->rgb()));
}
painter.drawText(QRect(horizontalMargin, row * boxSize + offSetFromTop, textWidth, boxSize),
Qt::AlignRight,
QString(alignment->getName(currentRow).c_str()));
}
}
}
}
/*
* Recompute maxNameLength based on the lengths of the identifier strings
* typeset in the current font at the current size, or
* DEFAULT_MAX_NAME_LENGTH, whichever is larger.
*
* Created: 16-02-07,Nigel Brown(EMBL)
**/
int SeqNameWidget::setMaxNameLength() {
//printf("setMaxNameLength(%0x)\n", (int)this);
maxNameLength = 0;
if (alignment) {
QFontMetrics fontMetrics(displayFont);
clustalw::Alignment::NameIterator i;
i.begin(alignment);
while (! i.end()) {
string name = i.next();
//cout << "name: " << name << endl;
int length = fontMetrics.width(QString(name.c_str()));
if (length > maxNameLength)
maxNameLength = length;
}
}
if (maxNameLength < 1)
maxNameLength = DEFAULT_MAX_NAME_LENGTH;
return maxNameLength;
}
void SeqNameWidget::changeBoxSizeAndFontSize(int _boxSize, QFont* font)
{
boxSize = _boxSize;
displayFont = *font;
setMaxNameLength();
resize(computeSize());
update();
}
void SeqNameWidget::clearSequenceSelection()
{
int sizeOfSelected = selectedRows.size();
for(int i = 0; i < sizeOfSelected; i++)
{
selectedRows[i] = 0;
}
update();
}
void SeqNameWidget::selectAllSequences()
{
int sizeOfSelected = selectedRows.size();
for(int i = 0; i < sizeOfSelected; i++)
{
selectedRows[i] = 1;
}
update();
}
int SeqNameWidget::getNumberOfSeqsSelected()
{
int num = 0;
int sizeOfSelected = selectedRows.size();
for(int i = 0; i < sizeOfSelected; i++)
{
if(selectedRows[i] == 1)
{
num++;
}
}
return num++;
}
int SeqNameWidget::getExtraRowNum() const //-nige
{
if (nHead != 1)
return 1;
return 2;
}
//nige 26-06-07
void SeqNameWidget::setNumSequences2(int count) {
//printf("RECV(SNW, setNumSequences2) %0x (%d)\n", (int)this, count);
numSequences2 = count;
}
|