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 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642
|
/****************************************************************************
**
** Copyright (C) 2006-2009 fullmetalcoder <fullmetalcoder@hotmail.fr>
**
** This file is part of the Edyuk project <http://edyuk.org>
**
** This file may be used under the terms of the GNU General Public License
** version 3 as published by the Free Software Foundation and appearing in the
** file GPL.txt included in the packaging of this file.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
****************************************************************************/
#include "qformatconfig.h"
/*!
\file qformatconfig.cpp
\brief Implementation of the QFormatConfig class.
\see QFormatConfig
*/
#include "qformat.h"
#include "qformatscheme.h"
#include "qeditor.h"
#include "qdocument.h"
#include "qsimplecolorpicker.h"
#include "utilsSystem.h"
/*!
\ingroup dialogs
@{
\class QFormatConfig
\brief A minimalistic, easy to embed, format settings widget.
*/
QFormatConfig::QFormatConfig(QWidget *w)
: QWidget(w), m_autonomous(false), m_currentScheme(0)
{
setupUi(this);
m_frame->hide();
m_table->verticalHeader()->hide();
m_table->setColumnCount(13);
for (int i=0;i<m_table->columnCount();i++)
if ( !m_table->horizontalHeaderItem(i) )
m_table->setHorizontalHeaderItem(i, new QTableWidgetItem());
Q_ASSERT(m_table->horizontalHeaderItem(0)!=0);
m_table->horizontalHeaderItem(0)->setText(tr("Identifier"));
m_table->horizontalHeaderItem(1)->setToolTip(tr("Bold"));
m_table->horizontalHeaderItem(1)->setIcon(getRealIcon("bold"));
m_table->horizontalHeaderItem(2)->setToolTip(tr("Italic"));
m_table->horizontalHeaderItem(2)->setIcon(getRealIcon("italic"));
m_table->horizontalHeaderItem(3)->setToolTip(tr("Underline"));
m_table->horizontalHeaderItem(3)->setIcon(getRealIcon("underline"));
m_table->horizontalHeaderItem(4)->setToolTip(tr("Overline"));
m_table->horizontalHeaderItem(4)->setIcon(getRealIcon("overline"));
m_table->horizontalHeaderItem(5)->setToolTip(tr("Strikeout"));
m_table->horizontalHeaderItem(5)->setIcon(getRealIcon("strikeout"));
m_table->horizontalHeaderItem(6)->setToolTip(tr("Wave underline"));
m_table->horizontalHeaderItem(6)->setIcon(getRealIcon("waveUnderline"));
m_table->horizontalHeaderItem(7)->setToolTip(tr("Text color (aka foreground)"));
m_table->horizontalHeaderItem(7)->setIcon(getRealIcon("format-text-color"));
m_table->horizontalHeaderItem(8)->setToolTip(tr("Background color"));
m_table->horizontalHeaderItem(8)->setIcon(getRealIcon("format-fill-color"));
m_table->horizontalHeaderItem(9)->setToolTip(tr("Lines color (used by all lines formatting : underline, overline, ...)"));
m_table->horizontalHeaderItem(9)->setIcon(getRealIcon("format-stroke-color"));
m_table->horizontalHeaderItem(10)->setText(tr("Font Family"));
m_table->horizontalHeaderItem(11)->setText(tr("Size")); // don't vary point size as the drwaing engine can't cope with it
m_table->horizontalHeaderItem(11)->setToolTip(tr("Font size relative to editor font size.\n\nNote: If the size is larger that the line spacing, characters may be clipped."));
m_table->horizontalHeaderItem(12)->setText(tr("Prio")); //TODO: images
m_table->horizontalHeaderItem(12)->setToolTip(tr("Priority determines which format is drawn on top, if multiple formats apply."));
#ifdef Q_OS_WIN
m_table->verticalHeader()->setDefaultSectionSize(21); // creates too high cells by default. TODO: use default height of a combobox instead of hard coding
#endif
m_table->setShowGrid(false);
#ifndef Q_OS_MAC
// workaround for bug: cell widgets not scrolling on OSX when stylesheet is set:
// https://bugreports.qt-project.org/browse/QTBUG-26674
// https://bugreports.qt-project.org/browse/QTBUG-25180
// https://bugreports.qt-project.org/browse/QTBUG-25148
// https://sourceforge.net/p/texstudio/bugs/615/
// https://sourceforge.net/p/texstudio/bugs/630/
m_table->setStyleSheet("QTableWidget {background-color: palette(window);}");
#endif
connect(m_table, SIGNAL( itemSelectionChanged() ),
m_table, SLOT ( clearSelection() ) );
basePointSize = 0;
}
/*!
\brief run-time translation entry point
*/
void QFormatConfig::retranslate()
{
retranslateUi(this);
}
/*!
\return Whether the format config widget is in "autonomous" mode
*/
bool QFormatConfig::isAutonomous() const
{
return m_autonomous;
}
/*!
\brief Turn on "autonomous" mode
When the format config widget is autonomous it will automatically
check for changes upon hide event and ask the user whether he wishes
to commit them.
*/
void QFormatConfig::setAutonomous(bool y)
{
m_autonomous = y;
}
/*!
\brief Check whether there are unsaved in the widget
*/
bool QFormatConfig::hasUnsavedChanges() const
{
return modifiedFormats().count();
}
/*!
\return the list of format schemes this config widget "manages"
*/
QList<QFormatScheme*> QFormatConfig::schemes() const
{
return m_schemes;
}
QList<QString>& QFormatConfig::addCategory(const QString& name){
m_categories.append(QList<QString>());
m_categories.last() << name;
return m_categories.last();
}
/*!
\brief Add a format scheme to the config widget
Users will be able to edit that scheme (switching among schemes using
a combobox if several schemes are added to the widget)
*/
void QFormatConfig::addScheme(const QString& name, QFormatScheme *scheme)
{
m_schemes << scheme;
m_selector->addItem(name);
if ( m_schemes.count() > 1 && m_schemes.contains(m_currentScheme) )
{
// show the scheme selector
m_frame->show();
}
//if ( !m_currentScheme ) not needed in txs
// setCurrentScheme(scheme);
}
/*!
\brief Remove a scheme from the config widget
\note No attempt is made to commit unsaved changes
*/
void QFormatConfig::removeScheme(QFormatScheme *s)
{
if ( m_currentScheme == s )
{
m_currentScheme = 0;
}
for ( int i = 0; i < m_schemes.count(); ++i )
{
if ( m_schemes.at(i) == s )
{
m_selector->removeItem(i);
m_schemes.removeAt(i);
}
}
if ( m_schemes.count() <= 1 )
{
// hide the scheme selector
m_frame->hide();
}
}
/*!
\brief Enforce the currently edited scheme
\note It is possible to pass a scheme not previously added to the widget,
but it really isn't recommended.
*/
void QFormatConfig::setCurrentScheme(QFormatScheme *s)
{
int idx = m_schemes.indexOf(s);
if ( idx != -1 )
{
m_currentScheme = s;
// update table widget
cancel();
} else {
m_selector->setCurrentIndex(idx);
}
}
void QFormatConfig::setBasePointSize(int basePointSize){
this->basePointSize = basePointSize?basePointSize:10;
}
/*!
\brief Apply changes made to the currently edited scheme, if any
*/
void QFormatConfig::apply()
{
if ( m_currentScheme )
{
for ( int i = 0 ; i < m_table->rowCount(); ++i )
{
QTableWidgetItem *item = m_table->item(i,0);
if (!item) continue;
QString fid = item->data(Qt::UserRole).toString();
if (fid == "") continue;
QFormat fmt = m_currentScheme->format(fid);
item = m_table->item(i, 1);
fmt.weight = item->checkState() == Qt::Checked ? QFont::Bold : QFont::Normal;
item = m_table->item(i, 2);
fmt.italic = item->checkState() == Qt::Checked;
item = m_table->item(i, 3);
fmt.underline = item->checkState() == Qt::Checked;
item = m_table->item(i, 4);
fmt.overline = item->checkState() == Qt::Checked;
item = m_table->item(i, 5);
fmt.strikeout = item->checkState() == Qt::Checked;
item = m_table->item(i, 6);
fmt.waveUnderline = item->checkState() == Qt::Checked;
QSimpleColorPicker *cp;
cp = qobject_cast<QSimpleColorPicker*>(m_table->cellWidget(i, 7));
if ( cp )
fmt.foreground = cp->color();
cp = qobject_cast<QSimpleColorPicker*>(m_table->cellWidget(i, 8));
fmt.background = cp->color();
cp = qobject_cast<QSimpleColorPicker*>(m_table->cellWidget(i, 9));
if ( cp )
fmt.linescolor = cp->color();
QComboBox *fcb;
fcb = qobject_cast<QComboBox*>(m_table->cellWidget(i, 10));
fmt.fontFamily = fcb->currentText()==tr("<default>") ? "" : fcb->currentText();
QDoubleSpinBox* sb= qobject_cast<QDoubleSpinBox*>(m_table->cellWidget(i, 11));
int newSize = qRound(sb->value()/100.0*basePointSize);
if (newSize == basePointSize) fmt.pointSize = 0;
else fmt.pointSize = newSize;
QSpinBox* psb= qobject_cast<QSpinBox*>(m_table->cellWidget(i, 12));
fmt.setPriority(psb->value());
m_currentScheme->setFormat(fid, fmt);
}
// TODO : save scheme and update editors
// this will only save schemes loaded from an existing file
m_currentScheme->save();
if ( m_autonomous )
{
QList<QEditor*> editors = QEditor::editors();
foreach ( QEditor *e, editors )
{
if ( e->document()->formatScheme() == m_currentScheme )
e->viewport()->update();
}
}
}
}
/*!
\brief Reset the subcontrols to reflect the data of the format scheme being edited
The name can be a bit misleading at first, it has been chosen
because it directly maps to the effect a "cancel" button would
have on the widget
*/
void QFormatConfig::cancel()
{
m_table->clearContents();
QTime T; T.start();
QFontDatabase database;
QStringList fonts = database.families();
fonts.insert(0, tr("<default>"));
if ( m_currentScheme )
{
if (m_categories.isEmpty()) {
QList<QString> &temp=addCategory("");
for ( int i = 0; i < m_currentScheme->formatCount(); i++ )
temp.append(m_currentScheme->id(i));
}
int n = 0;
foreach ( const QList<QString> c, m_categories)
n+=c.size();
n+=m_categories.size()-1;
m_table->setRowCount(n);
int r = 0;
for ( int c = 0; c < m_categories.size(); c++ ) {
if (c!=0) {
m_table->setItem(r, 0, new QTableWidgetItem());
m_table->setSpan(r++, 0, 1, 13);
}
QTableWidgetItem *item = new QTableWidgetItem(m_categories[c][0]);
QFont f = item->font(); f.setBold(true); item->setFont(f); item->setBackground(QApplication::palette().mid());
m_table->setItem(r, 0, item);
m_table->setSpan(r++, 0, 1, 13);
for ( int f = 1; f < m_categories[c].size(); f++ ) {
QString fid = m_categories[c][f];
const QFormat& fmt = m_currentScheme->format(fid);
QTableWidgetItem *item;
item = new QTableWidgetItem(tr(qPrintable(fid)));
item->setData(Qt::UserRole, fid);
item->setFlags(Qt::ItemIsEnabled);
m_table->setItem(r, 0, item);
item = new QTableWidgetItem;
item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsUserCheckable);
item->setCheckState(fmt.weight == QFont::Bold ? Qt::Checked : Qt::Unchecked);
item->setToolTip(m_table->horizontalHeaderItem(1)->toolTip());
m_table->setItem(r, 1, item);
item = new QTableWidgetItem;
item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsUserCheckable);
item->setCheckState(fmt.italic ? Qt::Checked : Qt::Unchecked);
item->setToolTip(m_table->horizontalHeaderItem(2)->toolTip());
m_table->setItem(r, 2, item);
item = new QTableWidgetItem;
item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsUserCheckable);
item->setCheckState(fmt.underline ? Qt::Checked : Qt::Unchecked);
item->setToolTip(m_table->horizontalHeaderItem(3)->toolTip());
m_table->setItem(r, 3, item);
item = new QTableWidgetItem;
item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsUserCheckable);
item->setCheckState(fmt.overline ? Qt::Checked : Qt::Unchecked);
item->setToolTip(m_table->horizontalHeaderItem(4)->toolTip());
m_table->setItem(r, 4, item);
item = new QTableWidgetItem;
item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsUserCheckable);
item->setCheckState(fmt.strikeout ? Qt::Checked : Qt::Unchecked);
item->setToolTip(m_table->horizontalHeaderItem(5)->toolTip());
m_table->setItem(r, 5, item);
item = new QTableWidgetItem;
item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsUserCheckable);
item->setCheckState(fmt.waveUnderline ? Qt::Checked : Qt::Unchecked);
item->setToolTip(m_table->horizontalHeaderItem(6)->toolTip());
m_table->setItem(r, 6, item);
m_table->setCellWidget(r, 7, new QSimpleColorPicker(fmt.foreground));
m_table->cellWidget(r, 7)->setToolTip(m_table->horizontalHeaderItem(7)->toolTip());
//m_table->cellWidget(i, 7)->setMaximumSize(22, 22);
m_table->setCellWidget(r, 8, new QSimpleColorPicker(fmt.background));
m_table->cellWidget(r, 8)->setToolTip(m_table->horizontalHeaderItem(8)->toolTip());
//m_table->cellWidget(i, 8)->setMaximumSize(22, 22);
m_table->setCellWidget(r, 9, new QSimpleColorPicker(fmt.linescolor));
m_table->cellWidget(r, 9)->setToolTip(m_table->horizontalHeaderItem(9)->toolTip());
//m_table->cellWidget(i, 9)->setMaximumSize(22, 22);
//item = new QTableWidgetItem;
QFontComboBox *fcmb=new QFontComboBox();
fcmb->insertItem(0, "<default>");
int ind=fcmb->findText(fmt.fontFamily);
if(ind>-1) fcmb->setCurrentIndex(ind);
else fcmb->setCurrentIndex(0);
//int ind=fcmb->findText(fmt.fontFamily);
//if(ind>-1) fcmb->setCurrentIndex(ind);
//else fcmb->setCurrentIndex(0);
//QComboBox *fcmb=new QComboBox();
//fcmb->addItems(fonts);
//int ind=fcmb->findText(fmt.fontFamily);
//if(ind>-1) fcmb->setCurrentIndex(ind);
//else fcmb->setCurrentIndex(0);
m_table->setCellWidget(r, 10, fcmb);
fcmb->setToolTip(m_table->horizontalHeaderItem(10)->toolTip());
QDoubleSpinBox* sb = new QDoubleSpinBox();
sb->setMaximum(100000);
sb->setMinimum(1);
sb->setDecimals(0);
sb->setSuffix("%");
sb->setAlignment(Qt::AlignRight);
sb->setFrame(false);
double v = fmt.pointSize?100.0*fmt.pointSize/basePointSize:100;
sb->setValue(v);
sb->setToolTip(m_table->horizontalHeaderItem(11)->toolTip());
m_table->setCellWidget(r, 11, sb);
QSpinBox *psb = new QSpinBox();
psb->setMaximum(100);
psb->setMinimum(-1);
psb->setAlignment(Qt::AlignRight);
psb->setFrame(false);
psb->setValue(fmt.priority);
psb->setToolTip(m_table->horizontalHeaderItem(12)->toolTip());
m_table->setCellWidget(r, 12, psb);
r++;
}
}
}
m_table->resizeColumnsToContents();
qDebug () << T.elapsed();
}
/*!
\brief Restore default values for all subcontrols
\note The widgets are changed but these changes are NOT applied.
*/
void QFormatConfig::restore()
{
// restoring what? this is only provided for API consistency and in case
// (very unlikely to ever happen) design changes make restore() a sensible
// thing to do on format schemes
}
QList<int> QFormatConfig::modifiedFormats() const
{
QList<int> hasModif;
if ( m_currentScheme )
{
const int n = m_currentScheme->formatCount();
m_table->setRowCount(n);
for ( int i = 0 ; i < n; ++i )
{
QString fid = m_currentScheme->id(i);
QFormat& fmt = m_currentScheme->formatRef(i);
QTableWidgetItem *item;
item = m_table->item(i, 1);
if ( fmt.weight != (item->checkState() == Qt::Checked ? QFont::Bold : QFont::Normal) )
{
hasModif << i;
continue;
}
item = m_table->item(i, 2);
if ( fmt.italic != (item->checkState() == Qt::Checked) )
{
hasModif << i;
continue;
}
item = m_table->item(i, 3);
if ( fmt.underline != (item->checkState() == Qt::Checked) )
{
hasModif << i;
continue;
}
item = m_table->item(i, 4);
if ( fmt.overline != (item->checkState() == Qt::Checked) )
{
hasModif << i;
continue;
}
item = m_table->item(i, 5);
if ( fmt.strikeout != (item->checkState() == Qt::Checked) )
{
hasModif << i;
continue;
}
item = m_table->item(i, 6);
if ( fmt.waveUnderline != (item->checkState() == Qt::Checked) )
{
hasModif << i;
continue;
}
QSimpleColorPicker *cp;
cp = qobject_cast<QSimpleColorPicker*>(m_table->cellWidget(i, 7));
if ( cp && fmt.foreground != cp->color() )
{
hasModif << i;
continue;
}
cp = qobject_cast<QSimpleColorPicker*>(m_table->cellWidget(i, 8));
if ( cp && fmt.background != cp->color() )
{
hasModif << i;
continue;
}
cp = qobject_cast<QSimpleColorPicker*>(m_table->cellWidget(i, 9));
if ( cp && fmt.linescolor != cp->color() )
{
hasModif << i;
continue;
}
QComboBox *fcb;
//item = m_table->item(i, 10);
fcb = qobject_cast<QComboBox*>(m_table->cellWidget(i, 10));
QString newFont=fcb->currentText()==tr("<default>") ? "" : fcb->currentText();
if (newFont != fmt.fontFamily){
hasModif << i;
continue;
}
QDoubleSpinBox* sb= qobject_cast<QDoubleSpinBox*>(m_table->cellWidget(i, 11));
if (qRound(sb->value()/100.0*basePointSize) != fmt.pointSize &&
(qRound(sb->value()/100.0*basePointSize) != basePointSize || fmt.pointSize != 0)){
hasModif << i;
continue;
}
QSpinBox* psb= qobject_cast<QSpinBox*>(m_table->cellWidget(i, 12));
if (psb->value() != fmt.priority) {
hasModif << i;
continue;
}
}
}
return hasModif;
}
void QFormatConfig::hideEvent(QHideEvent *e)
{
Q_UNUSED(e)
if ( !m_autonomous )
return;
QList<int> hasModif = modifiedFormats();
if ( hasModif.count() )
{
// TODO : provide custom widget to allow user to select which items should be saved?
int ret = QMessageBox::warning(
0,
tr("Unsaved changes"),
tr("There are unsaved changes in this format scheme.\nDo you want them to be saved?"),
QMessageBox::Save | QMessageBox::Discard
);
if ( ret == QMessageBox::Save )
{
apply();
} else {
cancel();
}
}
}
void QFormatConfig::showEvent(QShowEvent *e){
Q_UNUSED(e);
if (m_currentScheme || m_schemes.isEmpty())
return;
setCurrentScheme(m_schemes.first());
}
void QFormatConfig::on_m_selector_currentIndexChanged(int)
{
return; //not needed in txs
/*
QList<int> hasModif = modifiedFormats();
if ( hasModif.count() )
{
// TODO : provide custom widget to allow user to select which items should be saved?
int ret = QMessageBox::warning(
0,
tr("Unsaved changes"),
tr("There are unsaved changes in this format scheme.\nDo you want them to be saved?"),
QMessageBox::Save | QMessageBox::Discard
);
if ( ret == QMessageBox::Save )
{
apply();
} else {
cancel();
}
}
m_currentScheme = idx >= 0 && idx < m_schemes.count() ? m_schemes.at(idx) : 0;
cancel();*/
}
/*! @} */
|