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
|
// This contains the implementation of the pyqtSignal type.
//
// Copyright (c) 2010 Riverbank Computing Limited <info@riverbankcomputing.com>
//
// This file is part of PyQt.
//
// This file may be used under the terms of the GNU General Public
// License versions 2.0 or 3.0 as published by the Free Software
// Foundation and appearing in the files LICENSE.GPL2 and LICENSE.GPL3
// included in the packaging of this file. Alternatively you may (at
// your option) use any later version of the GNU General Public
// License if such license has been publicly approved by Riverbank
// Computing Limited (or its successors, if any) and the KDE Free Qt
// Foundation. In addition, as a special exception, Riverbank gives you
// certain additional rights. These rights are described in the Riverbank
// GPL Exception version 1.1, which can be found in the file
// GPL_EXCEPTION.txt in this package.
//
// Please review the following information to ensure GNU General
// Public Licensing requirements will be met:
// http://trolltech.com/products/qt/licenses/licensing/opensource/. If
// you are unsure which license is appropriate for your use, please
// review the following information:
// http://trolltech.com/products/qt/licenses/licensing/licensingoverview
// or contact the sales department at sales@riverbankcomputing.com.
//
// 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 <Python.h>
#include <QtGlobal>
#include <QByteArray>
#include <QMetaObject>
#include "qpycore_chimera.h"
#include "qpycore_misc.h"
#include "qpycore_pyqtboundsignal.h"
#include "qpycore_pyqtsignal.h"
// Forward declarations.
extern "C" {
static PyObject *pyqtSignal_call(PyObject *self, PyObject *args, PyObject *kw);
static void pyqtSignal_dealloc(PyObject *self);
static PyObject *pyqtSignal_descr_get(PyObject *self, PyObject *obj,
PyObject *type);
static int pyqtSignal_init(PyObject *self, PyObject *args, PyObject *kwd_args);
static PyObject *pyqtSignal_new(PyTypeObject *type, PyObject *args,
PyObject *kwd_args);
static PyObject *pyqtSignal_repr(PyObject *self);
static PyObject *pyqtSignal_get_doc(PyObject *self, void *);
}
static int add_overload(qpycore_pyqtSignal *ps, const char *name,
PyObject *types);
static int add_overload(qpycore_pyqtSignal *ps, const char *sig,
const char *docstring);
static bool is_signal_name(const char *sig, const char *name, uint name_len);
// The getters/setters.
static PyGetSetDef pyqtSignal_getsets[] = {
{(char *)"__doc__", pyqtSignal_get_doc, NULL, NULL, NULL},
{NULL, NULL, NULL, NULL, NULL}
};
PyDoc_STRVAR(pyqtSignal_doc,
"pyqtSignal(*types, name=str) -> signal\n"
"\n"
"types is normally a sequence of individual types. Each type is either a\n"
"type object or a string that is the name of a C++ type. Alternatively\n"
"each type could itself be a sequence of types each describing a different\n"
"overloaded signal.\n"
"name is the optional C++ name of the signal. If it is not specified then\n"
"the name of the class attribute that is bound to the signal is used.");
// The pyqtSignal type object.
PyTypeObject qpycore_pyqtSignal_Type = {
PyVarObject_HEAD_INIT(NULL, 0)
SIP_TPNAME_CAST("PyQt4.QtCore.pyqtSignal"), /* tp_name */
sizeof (qpycore_pyqtSignal), /* tp_basicsize */
0, /* tp_itemsize */
pyqtSignal_dealloc, /* tp_dealloc */
0, /* tp_print */
0, /* tp_getattr */
0, /* tp_setattr */
0, /* tp_compare */
pyqtSignal_repr, /* tp_repr */
0, /* tp_as_number */
0, /* tp_as_sequence */
0, /* tp_as_mapping */
0, /* tp_hash */
pyqtSignal_call, /* tp_call */
0, /* tp_str */
0, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT, /* tp_flags */
pyqtSignal_doc, /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
0, /* tp_methods */
0, /* tp_members */
pyqtSignal_getsets, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
pyqtSignal_descr_get, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
pyqtSignal_init, /* tp_init */
0, /* tp_alloc */
pyqtSignal_new, /* tp_new */
0, /* tp_free */
0, /* tp_is_gc */
0, /* tp_bases */
0, /* tp_mro */
0, /* tp_cache */
0, /* tp_subclasses */
0, /* tp_weaklist */
0, /* tp_del */
#if PY_VERSION_HEX >= 0x02060000
0, /* tp_version_tag */
#endif
};
// Get the docstring for a signal.
PyObject *qpycore_get_signal_doc(PyObject *self)
{
qpycore_pyqtSignal *ps = (qpycore_pyqtSignal *)self;
QByteArray doc;
// Get any docstrings from any non-signal overloads.
if (ps->non_signals && ps->non_signals->ml_doc)
{
doc.append('\n');
doc.append(ps->non_signals->ml_doc);
}
// Get any docstrings from the signals.
for (int i = 0; i < ps->overloads->size(); ++i)
{
const char *docstring = ps->overloads->at(i)->docstring;
if (docstring)
{
if (*docstring == '\1')
++docstring;
doc.append('\n');
doc.append(docstring);
doc.append(" [signal]");
}
}
if (doc.isEmpty())
{
Py_INCREF(Py_None);
return Py_None;
}
return
#if PY_MAJOR_VERSION >= 3
PyUnicode_FromString
#else
PyString_FromString
#endif
(doc.constData() + 1);
}
// The __doc__ getter.
static PyObject *pyqtSignal_get_doc(PyObject *self, void *)
{
return qpycore_get_signal_doc(self);
}
// The type repr slot.
static PyObject *pyqtSignal_repr(PyObject *self)
{
qpycore_pyqtSignal *ps = (qpycore_pyqtSignal *)self;
QByteArray name = Chimera::Signature::name(ps->overloads->first()->signature);
return
#if PY_MAJOR_VERSION >= 3
PyUnicode_FromFormat
#else
PyString_FromFormat
#endif
("<unbound signal %s>", name.constData() + 1);
}
// The type call slot.
static PyObject *pyqtSignal_call(PyObject *self, PyObject *args, PyObject *kw)
{
return qpycore_call_signal_overload(self, 0, args, kw);
}
// The type dealloc slot.
static void pyqtSignal_dealloc(PyObject *self)
{
qpycore_pyqtSignal *ps = (qpycore_pyqtSignal *)self;
if (ps->overloads)
{
qDeleteAll(ps->overloads->constBegin(), ps->overloads->constEnd());
delete ps->overloads;
ps->overloads = 0;
}
Py_TYPE(self)->tp_free(self);
}
// The type descriptor get slot.
static PyObject *pyqtSignal_descr_get(PyObject *self, PyObject *obj,
PyObject *)
{
qpycore_pyqtSignal *ps = (qpycore_pyqtSignal *)self;
// Return the unbound signal if there is nothing to bind it to.
if (obj == NULL || obj == Py_None)
{
Py_INCREF(self);
return self;
}
// Get the QObject.
int is_err = 0;
void *qobject = sipForceConvertToType(obj, sipType_QObject, 0,
SIP_NO_CONVERTORS, 0, &is_err);
if (is_err)
{
PyErr_Format(PyExc_TypeError,
"pyqtSignal must be bound to a QObject, not '%s'",
Py_TYPE(obj)->tp_name);
return 0;
}
// Return the bound signal.
return qpycore_pyqtBoundSignal_New(self, obj,
reinterpret_cast<QObject *>(qobject), ps->overloads->first());
}
// The type init slot.
static int pyqtSignal_init(PyObject *self, PyObject *args, PyObject *kwd_args)
{
qpycore_pyqtSignal *ps = (qpycore_pyqtSignal *)self;
// Get the keyword arguments.
PyObject *name_obj = 0;
const char *name = 0;
if (kwd_args)
{
SIP_SSIZE_T pos = 0;
PyObject *key, *value;
while (PyDict_Next(kwd_args, &pos, &key, &value))
{
#if PY_MAJOR_VERSION >= 3
if (PyUnicode_CompareWithASCIIString(key, "name") != 0)
{
PyErr_Format(PyExc_TypeError,
"pyqtSignal() got an unexpected keyword argument '%U'",
key);
Py_XDECREF(name_obj);
return -1;
}
#else
Q_ASSERT(PyString_Check(key));
if (qstrcmp(PyString_AS_STRING(key), "name") != 0)
{
PyErr_Format(PyExc_TypeError,
"pyqtSignal() got an unexpected keyword argument '%s'",
PyString_AS_STRING(key));
Py_XDECREF(name_obj);
return -1;
}
#endif
name_obj = value;
name = sipString_AsASCIIString(&name_obj);
if (!name)
return -1;
}
}
// If there is at least one argument and it is a sequence then assume all
// arguments are sequences. Unfortunately a string is also a sequence so
// check for tuples and lists explicitly.
if (PyTuple_GET_SIZE(args) > 0 && (PyTuple_Check(PyTuple_GET_ITEM(args, 0)) || PyList_Check(PyTuple_GET_ITEM(args, 0))))
{
for (SIP_SSIZE_T i = 0; i < PyTuple_GET_SIZE(args); ++i)
{
PyObject *types = PySequence_Tuple(PyTuple_GET_ITEM(args, i));
if (!types)
{
PyErr_SetString(PyExc_TypeError,
"pyqtSignal() argument expected to be sequence of types");
if (name)
{
Py_DECREF(name_obj);
}
return -1;
}
int rc = add_overload(ps, name, types);
Py_DECREF(types);
if (rc < 0)
{
if (name)
{
Py_DECREF(name_obj);
}
return -1;
}
}
}
else if (add_overload(ps, name, args) < 0)
{
if (name)
{
Py_DECREF(name_obj);
}
return -1;
}
if (name)
{
Py_DECREF(name_obj);
}
return 0;
}
// The type new slot.
static PyObject *pyqtSignal_new(PyTypeObject *type, PyObject *, PyObject *)
{
qpycore_pyqtSignal *ps;
ps = (qpycore_pyqtSignal *)type->tp_alloc(type, 0);
if (ps)
ps->overloads = new QList<Chimera::Signature *>;
return (PyObject *)ps;
}
// Add an overload when given a tuple of types.
static int add_overload(qpycore_pyqtSignal *ps, const char *name,
PyObject *types)
{
Chimera::Signature *parsed = Chimera::parse(types, name,
"a pyqtSignal() type argument");
if (!parsed)
return -1;
if (name)
parsed->signature.prepend('2');
ps->overloads->append(parsed);
return 0;
}
// Give a signal a name if it hasn't already got one.
void qpycore_set_signal_name(qpycore_pyqtSignal *ps, const char *name)
{
QList<Chimera::Signature *> *overloads = ps->overloads;
for (int i = 0; i < overloads->size(); ++i)
{
QByteArray &sig = overloads->at(i)->signature;
// If the signature already has a name then they all do and there is
// nothing more to do.
if (!sig.startsWith('('))
return;
sig.prepend(name);
sig.prepend('2');
}
}
// Handle the getting of a lazy attribute, ie. a native Qt signal.
int qpycore_get_lazy_attr(const sipTypeDef *td, PyObject *dict)
{
pyqt4ClassTypeDef *ctd = (pyqt4ClassTypeDef *)td;
const pyqt4QtSignal *sigs = ctd->qt4_signals;
// Handle the trvial case.
if (!sigs)
return 0;
QByteArray curr_name;
qpycore_pyqtSignal *curr = 0;
do
{
// See if we have come to the end of the current signal.
if (curr && !is_signal_name(sigs->signature, curr_name.constData(), curr_name.size()))
{
if (PyDict_SetItemString(dict, curr_name.constData(), (PyObject *)curr) < 0)
return -1;
curr = 0;
}
// See if we need to create a new signal.
if (!curr)
{
// Get the name.
curr_name = sigs->signature;
curr_name.truncate(curr_name.indexOf('('));
curr = (qpycore_pyqtSignal *)PyType_GenericAlloc(&qpycore_pyqtSignal_Type, 0);
if (!curr)
return -1;
curr->non_signals = sigs->non_signals;
curr->overloads = new QList<Chimera::Signature *>;
}
// Add the new overload.
if (add_overload(curr, sigs->signature, sigs->docstring) < 0)
{
Py_DECREF((PyObject *)curr);
return -1;
}
}
while ((++sigs)->signature);
// Save the last one.
return PyDict_SetItemString(dict, curr_name.constData(), (PyObject *)curr);
}
// Add an overload when given a native Qt signature.
static int add_overload(qpycore_pyqtSignal *ps, const char *sig,
const char *docstring)
{
QByteArray norm = QMetaObject::normalizedSignature(sig);
Chimera::Signature *parsed = Chimera::parse(norm, "a native Qt signal");
// This should never fail.
if (!parsed)
return -1;
parsed->signature.prepend('2');
parsed->docstring = docstring;
ps->overloads->append(parsed);
return 0;
}
// Return true if the signal has the given name.
static bool is_signal_name(const char *sig, const char *name, uint name_len)
{
return (qstrncmp(sig, name, name_len) == 0 && sig[name_len] == '(');
}
// Call a signal's overloaded method (if there is one).
PyObject *qpycore_call_signal_overload(PyObject *ps_obj, PyObject *bound,
PyObject *args, PyObject *kw)
{
qpycore_pyqtSignal *ps = (qpycore_pyqtSignal *)ps_obj;
if (!ps->non_signals)
{
PyErr_SetString(PyExc_TypeError, "native Qt signal is not callable");
return 0;
}
PyObject *func = PyCFunction_New(ps->non_signals, bound);
if (!func)
return 0;
PyObject *result = PyCFunction_Call(func, args, kw);
Py_DECREF(func);
return result;
}
|