Py::Object UiLoaderPy::createWidget(const Py::Tuple& args)
{
    Py::Module sipmod(PyImport_AddModule((char*)"sip"));
    Py::Module qtmod(PyImport_ImportModule((char*)"PyQt4.Qt"));

    // 1st argument
    std::string className = (std::string)Py::String(args[0]);

    // 2nd argument
    QWidget* parent = 0;
    if (args.size() > 1) {
        Py::Callable func = sipmod.getDict().getItem("unwrapinstance");
        Py::Tuple arguments(1);
        arguments[0] = args[1]; //PyQt pointer
        Py::Object result = func.apply(arguments);
        void* ptr = PyLong_AsVoidPtr(result.ptr());
        QObject* object = reinterpret_cast<QObject*>(ptr);
        if (object)
            parent = qobject_cast<QWidget*>(object);
    }

    // 3rd argument
    std::string objectName;
    if (args.size() > 2) {
        objectName = (std::string)Py::String(args[2]);
    }

    QWidget* widget = loader.createWidget(QString::fromAscii(className.c_str()), parent,
        QString::fromAscii(objectName.c_str()));
    Py::Callable func = sipmod.getDict().getItem("wrapinstance");
    Py::Tuple arguments(2);
    arguments[0] = Py::asObject(PyLong_FromVoidPtr(widget));
    arguments[1] = qtmod.getDict().getItem("QWidget");
    return func.apply(arguments);
}
Beispiel #2
0
Py::Object
_path_module::path_intersects_path(const Py::Tuple& args)
{
    args.verify_length(2, 3);

    PathIterator p1(args[0]);
    PathIterator p2(args[1]);
    bool filled = false;

    if (args.size() == 3)
    {
        filled = args[2].isTrue();
    }

    if (!filled)
    {
        return Py::Int(::path_intersects_path(p1, p2));
    }
    else
    {
        return Py::Int(::path_intersects_path(p1, p2)
                       || ::path_in_path(p1, agg::trans_affine(), p2, agg::trans_affine())
                       || ::path_in_path(p2, agg::trans_affine(), p1, agg::trans_affine()));
    }
}
Beispiel #3
0
 py::Object read ( py::Object self, py::Tuple args )
 try
 {
     std::istream& stream = *static_cast<std::istream*>
         (py::TypeBuilder::get_baton(self));
     std::ostringstream payload;
     if ( args.size() == 1 )
     {
         char data[1024];
         long size = py::Int(args[0]);
         for (; (size > 0); size -= stream.gcount()) {
             stream.read(data, std::min(size, long(sizeof(data))));
             payload.write(data, stream.gcount());
         }
     }
     else {
         payload << stream.rdbuf();
     }
     return (py::Bytes(payload.str()));
 }
 catch ( const std::bad_cast& )
 {
     std::cerr << "Bad cast!" << std::endl;
     return (py::None());
 }
Beispiel #4
0
Py::Object PythonSecurity::_getattr_(const Py::Tuple& args)
{
    krossdebug("PythonSecurity::_getattr_");
    for(uint i = 0; i < args.size(); i++) {
        Py::Object o = args[i];
        krossdebug( o.as_string().c_str() );
    }
    return Py::None();
}
Beispiel #5
0
 py::Object write ( py::Object self, py::Tuple args )
 {
     std::ostream& stream = *static_cast<std::ostream*>
         (py::TypeBuilder::get_baton(self));
     if (args.size() == 1)
     {
         stream << (std::string)py::Bytes(args[0]);
     }
     return (py::None());
 }
Beispiel #6
0
 py::Object writelines ( py::Object self, py::Tuple args )
 {
     std::ostream& stream = *static_cast<std::ostream*>
         (py::TypeBuilder::get_baton(self));
     if (args.size() == 1)
     {
         py::Iterator iterator(args[0]);
         while (iterator.next()) {
             stream << (std::string)py::Bytes(iterator.item());
         }
     }
     return (py::None());
 }
Beispiel #7
0
Py::Object CyPy_Task::irrelevant(const Py::Tuple& args)
{
    m_value->irrelevant();
    if (args.size() > 0) {
        args.verify_length(1);
        Atlas::Objects::Operation::Error e;
        Atlas::Objects::Entity::Anonymous arg;
        arg->setAttr("message", verifyString(args.front()));
        e->modifyArgs().push_back(arg);
        e->setTo(m_value->m_usageInstance.actor->getId());
        return CyPy_Operation::wrap(e);
    }
    return Py::None();
}
Beispiel #8
0
Py::Object PythonModule::import(const Py::Tuple& args)
{
    if(args.size() > 0) {
        QString modname = args[0].as_string().c_str();
        if(modname.startsWith("kross")) {
#ifdef KROSS_PYTHON_MODULE_DEBUG
            krossdebug( QString("Kross::Python::PythonModule::import() module=%1").arg(modname) );
#endif
            if( modname.find( QRegExp("[^a-zA-Z0-9\\_\\-]") ) >= 0 ) {
                krosswarning( QString("Denied import of Kross module '%1' cause of untrusted chars.").arg(modname) );
            }
            else {
                Kross::Api::Module::Ptr module = Kross::Api::Manager::scriptManager()->loadModule(modname);
                if(module)
                    return PythonExtension::toPyObject( Kross::Api::Object::Ptr(module) );
                krosswarning( QString("Loading of Kross module '%1' failed.").arg(modname) );
            }

        }
    }
    return Py::None();
}
Beispiel #9
0
// this code is heavily adapted from the paint license, which is in
// the file paint.license (BSD compatible) included in this
// distribution.  TODO, add license file to MANIFEST.in and CVS
Py::Object _png_module::write_png(const Py::Tuple& args)
{
    args.verify_length(4, 5);

    FILE *fp = NULL;
    bool close_file = false;
    bool close_dup_file = false;
    Py::Object buffer_obj = Py::Object(args[0]);
    PyObject* buffer = buffer_obj.ptr();
    if (!PyObject_CheckReadBuffer(buffer))
    {
        throw Py::TypeError("First argument must be an rgba buffer.");
    }

    const void* pixBufferPtr = NULL;
    Py_ssize_t pixBufferLength = 0;
    if (PyObject_AsReadBuffer(buffer, &pixBufferPtr, &pixBufferLength))
    {
        throw Py::ValueError("Couldn't get data from read buffer.");
    }

    png_byte* pixBuffer = (png_byte*)pixBufferPtr;
    int width = (int)Py::Int(args[1]);
    int height = (int)Py::Int(args[2]);

    if (pixBufferLength < width * height * 4)
    {
        throw Py::ValueError("Buffer and width, height don't seem to match.");
    }

    Py::Object py_fileobj = Py::Object(args[3]);
    PyObject* py_file = NULL;
    if (py_fileobj.isString())
    {
        if ((py_file = npy_PyFile_OpenFile(py_fileobj.ptr(), (char *)"wb")) == NULL) {
            throw Py::Exception();
        }
        close_file = true;
    }
    else
    {
        py_file = py_fileobj.ptr();
    }

    if ((fp = npy_PyFile_Dup(py_file, (char *)"wb")))
    {
        close_dup_file = true;
    }
    else
    {
        PyErr_Clear();
        PyObject* write_method = PyObject_GetAttrString(
                py_file, "write");
        if (!(write_method && PyCallable_Check(write_method)))
        {
            Py_XDECREF(write_method);
            throw Py::TypeError(
                    "Object does not appear to be a 8-bit string path or "
                    "a Python file-like object");
        }
        Py_XDECREF(write_method);
    }

    png_bytep *row_pointers = NULL;
    png_structp png_ptr = NULL;
    png_infop info_ptr = NULL;

    try
    {
        struct png_color_8_struct sig_bit;
        png_uint_32 row;

        row_pointers = new png_bytep[height];
        for (row = 0; row < (png_uint_32)height; ++row)
        {
            row_pointers[row] = pixBuffer + row * width * 4;
        }

        png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
        if (png_ptr == NULL)
        {
            throw Py::RuntimeError("Could not create write struct");
        }

        info_ptr = png_create_info_struct(png_ptr);
        if (info_ptr == NULL)
        {
            throw Py::RuntimeError("Could not create info struct");
        }

        if (setjmp(png_jmpbuf(png_ptr)))
        {
            throw Py::RuntimeError("Error building image");
        }

        if (fp)
        {
            png_init_io(png_ptr, fp);
        }
        else
        {
            png_set_write_fn(png_ptr, (void*)py_file,
                             &write_png_data, &flush_png_data);
        }
        png_set_IHDR(png_ptr, info_ptr,
                     width, height, 8,
                     PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE,
                     PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);

        // Save the dpi of the image in the file
        if (args.size() == 5)
        {
            double dpi = Py::Float(args[4]);
            size_t dots_per_meter = (size_t)(dpi / (2.54 / 100.0));
            png_set_pHYs(png_ptr, info_ptr, dots_per_meter, dots_per_meter, PNG_RESOLUTION_METER);
        }

        // this a a color image!
        sig_bit.gray = 0;
        sig_bit.red = 8;
        sig_bit.green = 8;
        sig_bit.blue = 8;
        /* if the image has an alpha channel then */
        sig_bit.alpha = 8;
        png_set_sBIT(png_ptr, info_ptr, &sig_bit);

        png_write_info(png_ptr, info_ptr);
        png_write_image(png_ptr, row_pointers);
        png_write_end(png_ptr, info_ptr);
    }
    catch (...)
    {
        if (png_ptr && info_ptr)
        {
            png_destroy_write_struct(&png_ptr, &info_ptr);
        }
        delete [] row_pointers;

        if (close_dup_file)
        {
            if (npy_PyFile_DupClose(py_file, fp)) {
              throw Py::RuntimeError("Error closing dupe file handle");
            }
        }

        if (close_file)
        {
            npy_PyFile_CloseFile(py_file);
            Py_DECREF(py_file);
        }
        /* Changed calls to png_destroy_write_struct to follow
           http://www.libpng.org/pub/png/libpng-manual.txt.
           This ensures the info_ptr memory is released.
        */
        throw;
    }

    png_destroy_write_struct(&png_ptr, &info_ptr);
    delete [] row_pointers;
    if (close_dup_file)
    {
        if (npy_PyFile_DupClose(py_file, fp)) {
          throw Py::RuntimeError("Error closing dupe file handle");
        }
    }

    if (close_file)
    {
        npy_PyFile_CloseFile(py_file);
        Py_DECREF(py_file);
    }

    if (PyErr_Occurred()) {
        throw Py::Exception();
    } else {
        return Py::Object();
    }
}