Changeset View
Changeset View
Standalone View
Standalone View
plugins/textshape/dialogs/StylesModel.cpp
Show All 22 Lines | |||||
23 | #include <KoStyleThumbnailer.h> | 23 | #include <KoStyleThumbnailer.h> | ||
24 | #include <KoStyleManager.h> | 24 | #include <KoStyleManager.h> | ||
25 | #include <KoParagraphStyle.h> | 25 | #include <KoParagraphStyle.h> | ||
26 | #include <KoCharacterStyle.h> | 26 | #include <KoCharacterStyle.h> | ||
27 | 27 | | |||
28 | #include <QImage> | 28 | #include <QImage> | ||
29 | #include <QList> | 29 | #include <QList> | ||
30 | #include <QSharedPointer> | 30 | #include <QSharedPointer> | ||
31 | #include <QSignalMapper> | | |||
32 | #include <QCollator> | 31 | #include <QCollator> | ||
33 | 32 | | |||
34 | #include <klocalizedstring.h> | 33 | #include <klocalizedstring.h> | ||
35 | #include <QDebug> | 34 | #include <QDebug> | ||
36 | 35 | | |||
37 | #include <algorithm> | 36 | #include <algorithm> | ||
38 | 37 | | |||
39 | StylesModel::StylesModel(KoStyleManager *manager, AbstractStylesModel::Type modelType, QObject *parent) | 38 | StylesModel::StylesModel(KoStyleManager *manager, AbstractStylesModel::Type modelType, QObject *parent) | ||
40 | : AbstractStylesModel(parent), | 39 | : AbstractStylesModel(parent), | ||
41 | m_styleManager(0), | 40 | m_styleManager(0), | ||
42 | m_currentParagraphStyle(0), | 41 | m_currentParagraphStyle(0), | ||
43 | m_defaultCharacterStyle(0), | 42 | m_defaultCharacterStyle(0), | ||
44 | m_styleMapper(new QSignalMapper(this)), | | |||
45 | m_provideStyleNone(false) | 43 | m_provideStyleNone(false) | ||
46 | { | 44 | { | ||
47 | m_modelType = modelType; | 45 | m_modelType = modelType; | ||
48 | setStyleManager(manager); | 46 | setStyleManager(manager); | ||
49 | //Create a default characterStyle for the preview of "None" character style | 47 | //Create a default characterStyle for the preview of "None" character style | ||
50 | if (m_modelType == StylesModel::CharacterStyle) { | 48 | if (m_modelType == StylesModel::CharacterStyle) { | ||
51 | m_defaultCharacterStyle = new KoCharacterStyle(); | 49 | m_defaultCharacterStyle = new KoCharacterStyle(); | ||
52 | m_defaultCharacterStyle->setStyleId(NoneStyleId); | 50 | m_defaultCharacterStyle->setStyleId(NoneStyleId); | ||
53 | m_defaultCharacterStyle->setName(i18n("None")); | 51 | m_defaultCharacterStyle->setName(i18n("None")); | ||
54 | m_defaultCharacterStyle->setFontPointSize(12); | 52 | m_defaultCharacterStyle->setFontPointSize(12); | ||
55 | 53 | | |||
56 | m_provideStyleNone = true; | 54 | m_provideStyleNone = true; | ||
57 | } | 55 | } | ||
58 | | ||||
59 | connect(m_styleMapper, SIGNAL(mapped(int)), this, SLOT(updateName(int))); | | |||
60 | } | 56 | } | ||
61 | 57 | | |||
62 | StylesModel::~StylesModel() | 58 | StylesModel::~StylesModel() | ||
63 | { | 59 | { | ||
64 | delete m_currentParagraphStyle; | 60 | delete m_currentParagraphStyle; | ||
65 | delete m_defaultCharacterStyle; | 61 | delete m_defaultCharacterStyle; | ||
66 | } | 62 | } | ||
67 | 63 | | |||
▲ Show 20 Lines • Show All 248 Lines • ▼ Show 20 Line(s) | 308 | for ( ; begin != m_styleList.end(); ++begin) { | |||
316 | // s should be found as the manager and the m_styleList should be in sync | 312 | // s should be found as the manager and the m_styleList should be in sync | ||
317 | Q_ASSERT(s); | 313 | Q_ASSERT(s); | ||
318 | if (collator.compare(style->name(),s->name()) < 0) { | 314 | if (collator.compare(style->name(),s->name()) < 0) { | ||
319 | break; | 315 | break; | ||
320 | } | 316 | } | ||
321 | ++index; | 317 | ++index; | ||
322 | } | 318 | } | ||
323 | beginInsertRows(QModelIndex(), index, index); | 319 | beginInsertRows(QModelIndex(), index, index); | ||
324 | m_styleList.insert(begin, style->styleId()); | 320 | int styleId = style->styleId(); | ||
325 | m_styleMapper->setMapping(style, style->styleId()); | 321 | m_styleList.insert(begin, styleId); | ||
326 | connect(style, SIGNAL(nameChanged(QString)), m_styleMapper, SLOT(map())); | 322 | connect(style, &KoParagraphStyle::nameChanged, this, [this, styleId] { updateName(styleId); }); | ||
327 | endInsertRows(); | 323 | endInsertRows(); | ||
328 | } | 324 | } | ||
329 | 325 | | |||
330 | bool sortParagraphStyleByName(KoParagraphStyle *style1, KoParagraphStyle *style2) | 326 | bool sortParagraphStyleByName(KoParagraphStyle *style1, KoParagraphStyle *style2) | ||
331 | { | 327 | { | ||
332 | Q_ASSERT(style1); | 328 | Q_ASSERT(style1); | ||
333 | Q_ASSERT(style2); | 329 | Q_ASSERT(style2); | ||
334 | return QCollator().compare(style1->name(), style2->name()) < 0; | 330 | return QCollator().compare(style1->name(), style2->name()) < 0; | ||
335 | } | 331 | } | ||
336 | 332 | | |||
337 | void StylesModel::updateParagraphStyles() | 333 | void StylesModel::updateParagraphStyles() | ||
338 | { | 334 | { | ||
339 | Q_ASSERT(m_styleManager); | 335 | Q_ASSERT(m_styleManager); | ||
340 | 336 | | |||
341 | beginResetModel(); | 337 | beginResetModel(); | ||
342 | m_styleList.clear(); | 338 | m_styleList.clear(); | ||
343 | 339 | | |||
344 | QList<KoParagraphStyle *> styles = m_styleManager->paragraphStyles(); | 340 | QList<KoParagraphStyle *> styles = m_styleManager->paragraphStyles(); | ||
345 | std::sort(styles.begin(), styles.end(), sortParagraphStyleByName); | 341 | std::sort(styles.begin(), styles.end(), sortParagraphStyleByName); | ||
346 | 342 | | |||
347 | foreach(KoParagraphStyle *style, styles) { | 343 | foreach(KoParagraphStyle *style, styles) { | ||
348 | if (style != m_styleManager->defaultParagraphStyle()) { //The default character style is not user selectable. It only provides individual property defaults and is not a style per say. | 344 | if (style != m_styleManager->defaultParagraphStyle()) { //The default character style is not user selectable. It only provides individual property defaults and is not a style per say. | ||
349 | m_styleList.append(style->styleId()); | 345 | int styleId = style->styleId(); | ||
350 | m_styleMapper->setMapping(style, style->styleId()); | 346 | m_styleList.append(styleId); | ||
351 | connect(style, SIGNAL(nameChanged(QString)), m_styleMapper, SLOT(map())); | 347 | connect(style, &KoParagraphStyle::nameChanged, this, [this, styleId] { updateName(styleId); }); | ||
352 | } | 348 | } | ||
353 | } | 349 | } | ||
354 | 350 | | |||
355 | endResetModel(); | 351 | endResetModel(); | ||
356 | } | 352 | } | ||
357 | 353 | | |||
358 | // called when the stylemanager adds a style | 354 | // called when the stylemanager adds a style | ||
359 | void StylesModel::addCharacterStyle(KoCharacterStyle *style) | 355 | void StylesModel::addCharacterStyle(KoCharacterStyle *style) | ||
Show All 17 Lines | 367 | for ( ; begin != m_styleList.constEnd(); ++begin) { | |||
377 | if (collator.compare(style->name(),s->name()) < 0) { | 373 | if (collator.compare(style->name(),s->name()) < 0) { | ||
378 | break; | 374 | break; | ||
379 | } | 375 | } | ||
380 | ++index; | 376 | ++index; | ||
381 | } | 377 | } | ||
382 | beginInsertRows(QModelIndex(), index, index); | 378 | beginInsertRows(QModelIndex(), index, index); | ||
383 | m_styleList.insert(index, style->styleId()); | 379 | m_styleList.insert(index, style->styleId()); | ||
384 | endInsertRows(); | 380 | endInsertRows(); | ||
385 | m_styleMapper->setMapping(style, style->styleId()); | 381 | int styleId = style->styleId(); | ||
386 | connect(style, SIGNAL(nameChanged(QString)), m_styleMapper, SLOT(map())); | 382 | connect(style, &KoParagraphStyle::nameChanged, this, [this, styleId] (const QString &) { updateName(styleId); }); | ||
387 | } | 383 | } | ||
388 | 384 | | |||
389 | bool sortCharacterStyleByName(KoCharacterStyle *style1, KoCharacterStyle *style2) | 385 | bool sortCharacterStyleByName(KoCharacterStyle *style1, KoCharacterStyle *style2) | ||
390 | { | 386 | { | ||
391 | Q_ASSERT(style1); | 387 | Q_ASSERT(style1); | ||
392 | Q_ASSERT(style2); | 388 | Q_ASSERT(style2); | ||
393 | return QCollator().compare(style1->name(), style2->name()) < 0; | 389 | return QCollator().compare(style1->name(), style2->name()) < 0; | ||
394 | } | 390 | } | ||
Show All 9 Lines | 399 | if (m_provideStyleNone && m_styleManager->paragraphStyles().count()) { | |||
404 | m_styleList.append(NoneStyleId); | 400 | m_styleList.append(NoneStyleId); | ||
405 | } | 401 | } | ||
406 | 402 | | |||
407 | QList<KoCharacterStyle *> styles = m_styleManager->characterStyles(); | 403 | QList<KoCharacterStyle *> styles = m_styleManager->characterStyles(); | ||
408 | std::sort(styles.begin(), styles.end(), sortCharacterStyleByName); | 404 | std::sort(styles.begin(), styles.end(), sortCharacterStyleByName); | ||
409 | 405 | | |||
410 | foreach(KoCharacterStyle *style, styles) { | 406 | foreach(KoCharacterStyle *style, styles) { | ||
411 | if (style != m_styleManager->defaultCharacterStyle()) { //The default character style is not user selectable. It only provides individual property defaults and is not a style per say. | 407 | if (style != m_styleManager->defaultCharacterStyle()) { //The default character style is not user selectable. It only provides individual property defaults and is not a style per say. | ||
412 | m_styleList.append(style->styleId()); | 408 | int styleId = style->styleId(); | ||
413 | m_styleMapper->setMapping(style, style->styleId()); | 409 | m_styleList.append(styleId); | ||
414 | connect(style, SIGNAL(nameChanged(QString)), m_styleMapper, SLOT(map())); | 410 | connect(style, &KoParagraphStyle::nameChanged, this, [this, styleId] { updateName(styleId); }); | ||
415 | } | 411 | } | ||
416 | } | 412 | } | ||
417 | 413 | | |||
418 | endResetModel(); | 414 | endResetModel(); | ||
419 | } | 415 | } | ||
420 | 416 | | |||
421 | // called when the stylemanager removes a style | 417 | // called when the stylemanager removes a style | ||
422 | void StylesModel::removeParagraphStyle(KoParagraphStyle *style) | 418 | void StylesModel::removeParagraphStyle(KoParagraphStyle *style) | ||
423 | { | 419 | { | ||
424 | int row = m_styleList.indexOf(style->styleId()); | 420 | int row = m_styleList.indexOf(style->styleId()); | ||
425 | beginRemoveRows(QModelIndex(), row, row); | 421 | beginRemoveRows(QModelIndex(), row, row); | ||
426 | m_styleMapper->removeMappings(style); | 422 | disconnect(style, &KoParagraphStyle::nameChanged, this, nullptr); | ||
427 | disconnect(style, SIGNAL(nameChanged(QString)), m_styleMapper, SLOT(map())); | | |||
428 | m_styleList.removeAt(row); | 423 | m_styleList.removeAt(row); | ||
429 | endRemoveRows(); | 424 | endRemoveRows(); | ||
430 | } | 425 | } | ||
431 | 426 | | |||
432 | // called when the stylemanager removes a style | 427 | // called when the stylemanager removes a style | ||
433 | void StylesModel::removeCharacterStyle(KoCharacterStyle *style) | 428 | void StylesModel::removeCharacterStyle(KoCharacterStyle *style) | ||
434 | { | 429 | { | ||
435 | int row = m_styleList.indexOf(style->styleId()); | 430 | int row = m_styleList.indexOf(style->styleId()); | ||
436 | beginRemoveRows(QModelIndex(), row, row); | 431 | beginRemoveRows(QModelIndex(), row, row); | ||
437 | m_styleMapper->removeMappings(style); | 432 | disconnect(style, &KoParagraphStyle::nameChanged, this, nullptr); | ||
438 | disconnect(style, SIGNAL(nameChanged(QString)), m_styleMapper, SLOT(map())); | | |||
439 | m_styleList.removeAt(row); | 433 | m_styleList.removeAt(row); | ||
440 | endRemoveRows(); | 434 | endRemoveRows(); | ||
441 | } | 435 | } | ||
442 | 436 | | |||
443 | void StylesModel::updateName(int styleId) | 437 | void StylesModel::updateName(int styleId) | ||
444 | { | 438 | { | ||
445 | // updating the name of a style can mean that the style needs to be moved inside the list to keep the sort order. | 439 | // updating the name of a style can mean that the style needs to be moved inside the list to keep the sort order. | ||
446 | QCollator collator; | 440 | QCollator collator; | ||
▲ Show 20 Lines • Show All 132 Lines • Show Last 20 Lines |