| 1 | // -*- c-basic-offset: 2 -*- |
|---|
| 2 | /* |
|---|
| 3 | * This file is part of Licq, an instant messaging client for UNIX. |
|---|
| 4 | * Copyright (C) 2000-2006 Licq developers |
|---|
| 5 | * |
|---|
| 6 | * Licq is free software; you can redistribute it and/or modify |
|---|
| 7 | * it under the terms of the GNU General Public License as published by |
|---|
| 8 | * the Free Software Foundation; either version 2 of the License, or |
|---|
| 9 | * (at your option) any later version. |
|---|
| 10 | * |
|---|
| 11 | * Licq is distributed in the hope that it will be useful, |
|---|
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 14 | * GNU General Public License for more details. |
|---|
| 15 | * |
|---|
| 16 | * You should have received a copy of the GNU General Public License |
|---|
| 17 | * along with Licq; if not, write to the Free Software |
|---|
| 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
|---|
| 19 | */ |
|---|
| 20 | |
|---|
| 21 | #include "selectemoticon.h" |
|---|
| 22 | |
|---|
| 23 | #include <math.h> |
|---|
| 24 | |
|---|
| 25 | #include <QGridLayout> |
|---|
| 26 | #include <QMap> |
|---|
| 27 | |
|---|
| 28 | #include "config/emoticons.h" |
|---|
| 29 | |
|---|
| 30 | #include "helpers/support.h" |
|---|
| 31 | |
|---|
| 32 | #include "emoticonlabel.h" |
|---|
| 33 | |
|---|
| 34 | using namespace LicqQtGui; |
|---|
| 35 | |
|---|
| 36 | SelectEmoticon::SelectEmoticon(QWidget* parent) |
|---|
| 37 | : QFrame(parent, Qt::Popup) |
|---|
| 38 | { |
|---|
| 39 | Support::setWidgetProps(this, "SelectEmoticon"); |
|---|
| 40 | setAttribute(Qt::WA_DeleteOnClose, true); |
|---|
| 41 | |
|---|
| 42 | setFrameShape(StyledPanel); |
|---|
| 43 | |
|---|
| 44 | QMap<QString, QString> map = Emoticons::self()->emoticonsKeys(); |
|---|
| 45 | QMap<QString, QString>::iterator iter; |
|---|
| 46 | |
|---|
| 47 | int nCols = static_cast<int>(sqrt(map.size())); |
|---|
| 48 | |
|---|
| 49 | grid = new QGridLayout(this); |
|---|
| 50 | grid->setContentsMargins(0, 0, 0, 0); |
|---|
| 51 | grid->setSpacing(0); |
|---|
| 52 | |
|---|
| 53 | int x = 0, y = 0; |
|---|
| 54 | for (iter = map.begin(); iter != map.end(); ++iter) |
|---|
| 55 | { |
|---|
| 56 | EmoticonLabel* lbl = new EmoticonLabel(iter.key(), iter.value(), this); |
|---|
| 57 | |
|---|
| 58 | connect(lbl, SIGNAL(clicked(const QString&)), SLOT(emoticonClicked(const QString&))); |
|---|
| 59 | connect(lbl, SIGNAL(move(EmoticonLabel*, int)), SLOT(moveFrom(EmoticonLabel*, int))); |
|---|
| 60 | |
|---|
| 61 | grid->addWidget(lbl, y, x++); |
|---|
| 62 | grid->setAlignment(lbl, Qt::AlignCenter); |
|---|
| 63 | |
|---|
| 64 | // Set the focus to the first item so we can use the keyboard to navigate |
|---|
| 65 | if (y == 0 && x == 1) |
|---|
| 66 | lbl->setFocus(); |
|---|
| 67 | |
|---|
| 68 | if (x == nCols) |
|---|
| 69 | { |
|---|
| 70 | x = 0; |
|---|
| 71 | y++; |
|---|
| 72 | } |
|---|
| 73 | } |
|---|
| 74 | } |
|---|
| 75 | |
|---|
| 76 | void SelectEmoticon::emoticonClicked(const QString& value) |
|---|
| 77 | { |
|---|
| 78 | emit selected(value); |
|---|
| 79 | close(); |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | void SelectEmoticon::moveFrom(EmoticonLabel* item, int key) |
|---|
| 83 | { |
|---|
| 84 | if (item == 0) |
|---|
| 85 | return; |
|---|
| 86 | |
|---|
| 87 | int index = grid->indexOf(item); |
|---|
| 88 | |
|---|
| 89 | switch (key) |
|---|
| 90 | { |
|---|
| 91 | case Qt::Key_Up: |
|---|
| 92 | index -= grid->columnCount(); |
|---|
| 93 | if (index < 0) |
|---|
| 94 | index += grid->columnCount() * grid->rowCount(); |
|---|
| 95 | while (grid->itemAt(index) == 0) |
|---|
| 96 | index -= grid->columnCount(); |
|---|
| 97 | break; |
|---|
| 98 | |
|---|
| 99 | case Qt::Key_Down: |
|---|
| 100 | index += grid->columnCount(); |
|---|
| 101 | while (grid->itemAt(index) == 0) |
|---|
| 102 | if (index >= grid->columnCount() * grid->rowCount()) |
|---|
| 103 | index -= grid->columnCount() * grid->rowCount(); |
|---|
| 104 | else |
|---|
| 105 | index += grid->columnCount(); |
|---|
| 106 | break; |
|---|
| 107 | |
|---|
| 108 | default: |
|---|
| 109 | return; |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | grid->itemAt(index)->widget()->setFocus(); |
|---|
| 113 | } |
|---|