~sil/ubuntu-keyboard/numbers-on-top-row

« back to all changes in this revision

Viewing changes to src/lib/logic/layoutupdater.cpp

  • Committer: Guenter Schwann
  • Date: 2013-11-13 15:36:39 UTC
  • mfrom: (101.4.5 keyboard-cleanups)
  • Revision ID: guenter.schwann@canonical.com-20131113153639-9i9irt1mle00vy1t
Remove some dead code

Show diffs side-by-side

added added

removed removed

Lines of Context:
40
40
#include "models/text.h"
41
41
#include "models/styleattributes.h"
42
42
 
43
 
#include "logic/keyareaconverter.h"
44
 
#include "logic/state-machines/shiftmachine.h"
45
 
#include "logic/state-machines/viewmachine.h"
46
 
#include "logic/state-machines/deadkeymachine.h"
47
 
 
48
43
namespace MaliitKeyboard {
49
44
namespace Logic {
50
45
 
74
69
                           LayoutHelper::Orientation orientation,
75
70
                           ActivationPolicy policy)
76
71
{
77
 
    if (not candidate || not attributes) {
78
 
        return;
79
 
    }
80
 
 
81
 
    Label &label(candidate->rLabel());
82
 
    Font f(label.font());
83
 
    f.setSize(attributes->candidateFontSize(orientation));
84
 
    f.setStretch(attributes->candidateFontStretch(orientation));
85
 
 
86
 
    QByteArray color;
87
 
    switch(policy) {
88
 
    case ActivateElement:
89
 
        color = QByteArray("#fff");
90
 
        break;
91
 
 
92
 
    case DeactivateElement:
93
 
        color = QByteArray("#ddd");
94
 
        break;
95
 
    }
96
 
 
97
 
    f.setColor(color);
98
 
    label.setFont(f);
 
72
    Q_UNUSED(candidate);
 
73
    Q_UNUSED(attributes);
 
74
    Q_UNUSED(orientation);
 
75
    Q_UNUSED(policy);
99
76
}
100
77
 
101
78
// FIXME: Make word candidates fit word ribbon also after orientation change.
130
107
    for (int index = 0; index < candidates.count(); ++index) {
131
108
        WordCandidate &current(candidates[index]);
132
109
 
133
 
        if (current.label().text() == candidate.label().text()) {
 
110
        if (current.label() == candidate.label()) {
134
111
            // in qml we don´t care about ( current.rect() == candidate.rect() )
135
112
            applyStyleToCandidate(&current, attributes, layout->orientation(), policy);
136
113
            layout->setWordRibbon(layout->wordRibbon());
152
129
               LayoutHelper::Orientation orientation,
153
130
               const QRectF &key_area_rect)
154
131
{
155
 
    Font magnifier_font;
156
 
    magnifier_font.setName(attributes->fontName(orientation));
157
 
    magnifier_font.setColor(attributes->fontColor(orientation));
158
 
    magnifier_font.setSize(attributes->magnifierFontSize(orientation));
159
 
 
160
132
    if (key.action() != Key::ActionInsert) {
161
133
        return Key();
162
134
    }
189
161
    magnifier.rArea().setBackground(attributes->magnifierKeyBackground());
190
162
    magnifier.rArea().setSize(magnifier_rect.size());
191
163
    magnifier.rArea().setBackgroundBorders(attributes->magnifierKeyBackgroundBorders());
192
 
    magnifier.rLabel().setFont(magnifier_font);
193
164
 
194
 
    // Compute label rectangle, contains the text:
195
 
    const qreal label_offset(attributes->magnifierKeyLabelVerticalOffset(orientation));
196
 
    const QSize &magnifier_size(magnifier.area().size());
197
 
    const QRect label_rect(0, 0,
198
 
                           magnifier_size.width(),
199
 
                           magnifier_size.height() - label_offset);
200
 
    magnifier.rLabel().setRect(label_rect);
201
165
    magnifier.setMargins(QMargins());
202
166
 
203
167
    return magnifier;
209
173
    bool initialized;
210
174
    LayoutHelper *layout;
211
175
    KeyboardLoader loader;
212
 
    ShiftMachine shift_machine;
213
 
    ViewMachine view_machine;
214
 
    DeadkeyMachine deadkey_machine;
215
176
    SharedStyle style;
216
177
    bool word_ribbon_visible;
217
178
    LayoutHelper::Panel close_extended_on_release;
220
181
        : initialized(false)
221
182
        , layout(0)
222
183
        , loader()
223
 
        , shift_machine()
224
 
        , view_machine()
225
 
        , deadkey_machine()
226
184
        , style()
227
185
        , word_ribbon_visible(false)
228
186
        , close_extended_on_release(LayoutHelper::NumPanels) // NumPanels counts as invalid panel.
230
188
 
231
189
    bool inShiftedState() const
232
190
    {
233
 
        return (shift_machine.inState(ShiftMachine::shift_state) or
234
 
                shift_machine.inState(ShiftMachine::caps_lock_state) or
235
 
                shift_machine.inState(ShiftMachine::latched_shift_state));
 
191
        return false;
236
192
    }
237
193
 
238
194
    bool arePrimarySymbolsShown() const
239
195
    {
240
 
        return view_machine.inState(ViewMachine::symbols0_state);
 
196
        return false;
241
197
    }
242
198
 
243
199
    bool areSecondarySymbolsShown() const
244
200
    {
245
 
        return view_machine.inState(ViewMachine::symbols1_state);
 
201
        return false;
246
202
    }
247
203
 
248
204
    bool areSymbolsShown() const
252
208
 
253
209
    bool inDeadkeyState() const
254
210
    {
255
 
        return (deadkey_machine.inState(DeadkeyMachine::deadkey_state) or
256
 
                deadkey_machine.inState(DeadkeyMachine::latched_deadkey_state));
 
211
        return false;
257
212
    }
258
213
 
259
214
    const StyleAttributes * activeStyleAttributes() const
277
232
 
278
233
void LayoutUpdater::init()
279
234
{
280
 
    Q_D(LayoutUpdater);
281
 
 
282
 
    d->shift_machine.setup(this);
283
 
    d->view_machine.setup(this);
284
 
    d->deadkey_machine.setup(this);
285
235
}
286
236
 
287
237
QStringList LayoutUpdater::keyboardIds() const
322
272
 
323
273
void LayoutUpdater::setOrientation(LayoutHelper::Orientation orientation)
324
274
{
325
 
    Q_D(LayoutUpdater);
326
 
 
327
 
    if (d->layout && d->style && d->layout->orientation() != orientation) {
328
 
        d->layout->setOrientation(orientation);
329
 
 
330
 
        KeyAreaConverter converter(d->style->attributes(), &d->loader);
331
 
        converter.setLayoutOrientation(orientation);
332
 
        d->layout->setCenterPanel(d->inShiftedState() ? converter.shiftedKeyArea()
333
 
                                                      : converter.keyArea());
334
 
 
335
 
        if (isWordRibbonVisible())
336
 
            applyStyleToWordRibbon(d->layout->wordRibbon(), d->style, orientation);
337
 
 
338
 
        clearActiveKeysAndMagnifier();
339
 
    }
 
275
    Q_UNUSED(orientation);
340
276
}
341
277
 
342
278
void LayoutUpdater::setStyle(const SharedStyle &style)
401
337
        break;
402
338
 
403
339
    case Key::ActionDead:
404
 
        d->deadkey_machine.setAccentKey(key);
405
340
        Q_EMIT deadkeyPressed();
406
341
        break;
407
342
 
413
348
void LayoutUpdater::onKeyLongPressed(const Key &key)
414
349
{
415
350
    Q_UNUSED(key);
416
 
    Q_D(LayoutUpdater);
417
 
 
418
 
    if (not d->layout || d->style.isNull()) {
419
 
        return;
420
 
    }
421
 
 
422
 
    clearActiveKeysAndMagnifier();
423
 
 
424
 
    const LayoutHelper::Orientation orientation(d->layout->orientation());
425
 
    StyleAttributes * const extended_attributes(d->style->extendedKeysAttributes());
426
 
    const qreal vertical_offset(d->style->attributes()->verticalOffset(orientation));
427
 
    KeyAreaConverter converter(extended_attributes, &d->loader);
428
 
    converter.setLayoutOrientation(orientation);
429
 
    KeyArea ext_ka(converter.extendedKeyArea(key));
430
 
 
431
 
    if (not ext_ka.hasKeys()) {
432
 
        if (key.action() == Key::ActionSpace) {
433
 
            Q_EMIT addToUserDictionary();
434
 
        }
435
 
        return;
436
 
    }
437
 
 
438
 
    const QSize &ext_panel_size(ext_ka.area().size());
439
 
    const QSize &center_panel_size(d->layout->centerPanel().area().size());
440
 
    const QPointF &key_center(key.rect().center());
441
 
    const qreal safety_margin(extended_attributes->safetyMargin(orientation));
442
 
 
443
 
    QPoint offset(qMax<int>(safety_margin, key_center.x() - ext_panel_size.width() / 2),
444
 
                  key.rect().top() - vertical_offset);
445
 
 
446
 
    if (offset.x() + ext_panel_size.width() > center_panel_size.width()) {
447
 
        offset.rx() = center_panel_size.width() - ext_panel_size.width() - safety_margin;
448
 
    }
449
 
 
450
 
    ext_ka.setOrigin(offset);
451
 
    d->layout->setExtendedPanel(ext_ka);
452
 
    d->layout->setActivePanel(LayoutHelper::ExtendedPanel);
453
351
}
454
352
 
455
353
void LayoutUpdater::onKeyReleased(const Key &key)
456
354
{
457
 
    Q_D(const LayoutUpdater);
458
 
 
459
 
    if (not d->layout) {
460
 
        return;
461
 
    }
462
 
 
463
 
    d->layout->removeActiveKey(key);
464
 
    d->layout->clearMagnifierKey();
465
 
 
466
 
    if (d->layout->activePanel() == LayoutHelper::ExtendedPanel) {
467
 
        d->layout->clearActiveKeys();
468
 
        d->layout->setExtendedPanel(KeyArea());
469
 
        d->layout->setActivePanel(LayoutHelper::CenterPanel);
470
 
        return;
471
 
    }
472
 
 
473
 
    switch (key.action()) {
474
 
    case Key::ActionShift:
475
 
        Q_EMIT shiftReleased();
476
 
        break;
477
 
 
478
 
    case Key::ActionInsert:
479
 
        if (d->shift_machine.inState(ShiftMachine::latched_shift_state)) {
480
 
            Q_EMIT shiftCancelled();
481
 
        }
482
 
 
483
 
        if (d->deadkey_machine.inState(DeadkeyMachine::latched_deadkey_state)) {
484
 
            Q_EMIT deadkeyCancelled();
485
 
        }
486
 
 
487
 
        break;
488
 
 
489
 
    case Key::ActionSym:
490
 
        Q_EMIT symKeyReleased();
491
 
        break;
492
 
 
493
 
    case Key::ActionSwitch:
494
 
        Q_EMIT symSwitcherReleased();
495
 
        break;
496
 
 
497
 
    case Key::ActionDead:
498
 
        Q_EMIT deadkeyReleased();
499
 
        break;
500
 
 
501
 
    default:
502
 
        break;
503
 
    }
 
355
    Q_UNUSED(key);
504
356
}
505
357
 
506
358
void LayoutUpdater::onKeyAreaPressed(LayoutHelper::Panel panel)
614
466
 
615
467
void LayoutUpdater::onExtendedKeysShown(const Key &main_key)
616
468
{
617
 
    Q_D(LayoutUpdater);
618
 
 
619
 
    if (not d->layout || d->style.isNull()) {
620
 
        return;
621
 
    }
622
 
 
623
 
    clearActiveKeysAndMagnifier();
624
 
 
625
 
    const LayoutHelper::Orientation orientation(d->layout->orientation());
626
 
    StyleAttributes * const extended_attributes(d->style->extendedKeysAttributes());
627
 
    const qreal vertical_offset(d->style->attributes()->verticalOffset(orientation));
628
 
    KeyAreaConverter converter(extended_attributes, &d->loader);
629
 
    converter.setLayoutOrientation(orientation);
630
 
    KeyArea ext_ka(converter.extendedKeyArea(main_key));
631
 
 
632
 
    if (not ext_ka.hasKeys()) {
633
 
        if (main_key.action() == Key::ActionSpace) {
634
 
            Q_EMIT addToUserDictionary();
635
 
        }
636
 
        return;
637
 
    }
638
 
 
639
 
    const QSize &ext_panel_size(ext_ka.area().size());
640
 
    const QSize &center_panel_size(d->layout->centerPanel().area().size());
641
 
    const QPointF &key_center(main_key.rect().center());
642
 
    const qreal safety_margin(extended_attributes->safetyMargin(orientation));
643
 
 
644
 
    QPoint offset(qMax<int>(safety_margin, key_center.x() - ext_panel_size.width() / 2),
645
 
                  main_key.rect().top() - vertical_offset);
646
 
 
647
 
    if (offset.x() + ext_panel_size.width() > center_panel_size.width()) {
648
 
        offset.rx() = center_panel_size.width() - ext_panel_size.width() - safety_margin;
649
 
    }
650
 
 
651
 
    ext_ka.setOrigin(offset);
652
 
    d->layout->setExtendedPanel(ext_ka);
653
 
    d->layout->setActivePanel(LayoutHelper::ExtendedPanel);
 
469
    Q_UNUSED(main_key);
654
470
}
655
471
 
656
472
void LayoutUpdater::onWordCandidatePressed(const WordCandidate &candidate)
703
519
{
704
520
    Q_D(LayoutUpdater);
705
521
 
706
 
    // Resetting state machines should reset layout also.
707
 
    // FIXME: Most probably reloading will happen three
708
 
    // times, which is not what we want.
709
 
    d->shift_machine.restart();
710
 
    d->deadkey_machine.restart();
711
 
    d->view_machine.restart();
712
 
 
713
522
    Q_EMIT keyboardTitleChanged(d->loader.title(d->loader.activeId()));
714
523
}
715
524
 
716
525
void LayoutUpdater::switchToMainView()
717
526
{
718
 
    Q_D(LayoutUpdater);
719
 
 
720
 
    if (not d->layout || d->style.isNull()) {
721
 
        return;
722
 
    }
723
 
 
724
 
    d->layout->clearActiveKeys();
725
 
    d->layout->clearMagnifierKey();
726
 
 
727
 
    const LayoutHelper::Orientation orientation(d->layout->orientation());
728
 
 
729
 
    if (d->word_ribbon_visible)
730
 
        applyStyleToWordRibbon(d->layout->wordRibbon(), d->style, orientation);
731
 
 
732
 
    KeyAreaConverter converter(d->style->attributes(), &d->loader);
733
 
    converter.setLayoutOrientation(orientation);
734
 
    d->layout->setCenterPanel(d->inShiftedState() ? converter.shiftedKeyArea()
735
 
                                                  : converter.keyArea());
736
 
 
737
 
    if (d->inShiftedState())
738
 
        Q_EMIT d->layout->stateChanged(Model::Layout::ShiftedState);
739
 
    else if (d->inDeadkeyState())
740
 
        Q_EMIT d->layout->stateChanged(Model::Layout::DeadkeyState);
741
 
    else
742
 
        Q_EMIT d->layout->stateChanged(Model::Layout::DefaultState);
743
527
}
744
528
 
745
529
void LayoutUpdater::switchToPrimarySymView()
746
530
{
747
 
    Q_D(LayoutUpdater);
748
 
 
749
 
    if (not d->layout || d->style.isNull()) {
750
 
        return;
751
 
    }
752
 
 
753
 
    const LayoutHelper::Orientation orientation(d->layout->orientation());
754
 
    KeyAreaConverter converter(d->style->attributes(), &d->loader);
755
 
    converter.setLayoutOrientation(orientation);
756
 
    d->layout->setCenterPanel(converter.symbolsKeyArea(0));
757
 
 
758
 
    // Reset shift state machine, also see switchToMainView.
759
 
    d->shift_machine.restart();
760
 
 
761
 
    Q_EMIT d->layout->stateChanged(Model::Layout::PrimarySymbolState);
762
531
}
763
532
 
764
533
void LayoutUpdater::switchToSecondarySymView()
765
534
{
766
 
    Q_D(LayoutUpdater);
767
 
 
768
 
    if (not d->layout || d->style.isNull()) {
769
 
        return;
770
 
    }
771
 
 
772
 
    const LayoutHelper::Orientation orientation(d->layout->orientation());
773
 
    KeyAreaConverter converter(d->style->attributes(), &d->loader);
774
 
    converter.setLayoutOrientation(orientation);
775
 
    d->layout->setCenterPanel(converter.symbolsKeyArea(1));
776
 
 
777
 
    Q_EMIT d->layout->stateChanged(Model::Layout::SecondarySymbolState);
778
535
}
779
536
 
780
537
void LayoutUpdater::switchToAccentedView()
781
538
{
782
 
    Q_D(LayoutUpdater);
783
 
 
784
 
    if (not d->layout || d->style.isNull()) {
785
 
        return;
786
 
    }
787
 
 
788
 
 
789
 
    const LayoutHelper::Orientation orientation(d->layout->orientation());
790
 
    KeyAreaConverter converter(d->style->attributes(), &d->loader);
791
 
    converter.setLayoutOrientation(orientation);
792
 
    const Key accent(d->deadkey_machine.accentKey());
793
 
    d->layout->setCenterPanel(d->inShiftedState() ? converter.shiftedDeadKeyArea(accent)
794
 
                                                  : converter.deadKeyArea(accent));
795
539
}
796
540
 
797
541
}} // namespace Logic, MaliitKeyboard