QByteArray bitsToBytes(QBitArray bits) { QByteArray bytes; bytes.resize(qCeil(bits.count() / 8.0)); bytes.fill(0); for(int b = 0; b < bits.count(); ++b) { bytes[b / 8] = (bytes.at(b / 8) | ((bits[b] ? 1 : 0) << (b % 8))); } return bytes; }
void tst_QBitArray::countBits2() { QBitArray bitArray; for (int i = 0; i < 4017; ++i) { bitArray.resize(i); bitArray.fill(true); QCOMPARE(bitArray.count(true), i); QCOMPARE(bitArray.count(false), 0); bitArray.fill(false); QCOMPARE(bitArray.count(true), 0); QCOMPARE(bitArray.count(false), i); } }
// Sends the complete list of pieces that we have downloaded. void PeerWireClient::sendPieceList(const QBitArray &bitField) { // The bitfield message may only be sent immediately after the // handshaking sequence is completed, and before any other // messages are sent. if (!sentHandShake) sendHandShake(); // Don't send the bitfield if it's all zeros. if (bitField.count(true) == 0) return; int bitFieldSize = bitField.size(); int size = (bitFieldSize + 7) / 8; QByteArray bits(size, '\0'); for (int i = 0; i < bitFieldSize; ++i) { if (bitField.testBit(i)) { quint32 byte = quint32(i) / 8; quint32 bit = quint32(i) % 8; bits[byte] = uchar(bits.at(byte)) | (1 << (7 - bit)); } } char message[] = {0, 0, 0, 1, 5}; toNetworkData(bits.size() + 1, &message[0]); write(message, sizeof(message)); write(bits); }
void KisSaveXmlVisitor::saveLayer(QDomElement & el, const QString & layerType, const KisLayer * layer) { QString channelFlagsString; QBitArray channelFlags = layer->channelFlags(); for ( int i = 0; i < channelFlags.count(); ++i ) { if ( channelFlags[i] ) channelFlagsString += '1'; else channelFlagsString += '0'; } el.setAttribute( CHANNEL_FLAGS, channelFlagsString ); el.setAttribute(NAME, layer->name()); el.setAttribute(OPACITY, layer->opacity()); el.setAttribute(COMPOSITE_OP, layer->compositeOp()->id()); el.setAttribute(VISIBLE, layer->visible()); el.setAttribute(LOCKED, layer->userLocked()); el.setAttribute(NODE_TYPE, layerType); el.setAttribute(FILE_NAME, LAYER + QString::number( m_count ) ); el.setAttribute(X, layer->x()); el.setAttribute(Y, layer->y()); m_nodeFileNames[layer] = LAYER + QString::number( m_count ); dbgFile << "Saved layer " << layer->name() << " of type " << layerType << " with filename " << LAYER + QString::number( m_count ); }
// Run: bool OKFilter::execute(const QList<QDir> &directories, const QString &srcFile) { if (directories.size() < 2) return false; // Get reference file: QString refFile = directories.first().absoluteFilePath(srcFile); QImage img(refFile); int nPixels = img.width()*img.height(); int threshold = qRound(.5*nPixels); // Get reference state: QBitArray refState = imageToVect(refFile); for (int i=1; i<directories.size(); i++) { // Exists? QString file = directories[i].absoluteFilePath(srcFile); QBitArray currentState = imageToVect(file); QBitArray tmp = (~refState & currentState) | (refState & ~currentState); int nErrors = tmp.count(true); if (nErrors > threshold) return false; } return true; }
QList<QWidget *> FatherBlock::GetEnableBlocksWidget(int theStatusIndex) { QList<QWidget *>theBlockWidgets; if(theStatusIndex == -1) { theStatusIndex = m_current_status_index; } if(theStatusIndex < m_status_list.count()) { QBitArray theStatus = m_status_list[theStatusIndex]; int count = theStatus.count()-1; while(count >= 0) { if(theStatus.testBit(count) == true) { QWidget *ChildBlockWidget = GetSingleBlock(m_block_type); ChildBlockWidget->setStyleSheet(m_BlockStyleSheet); ChildBlockWidget->setGeometry(m_blocks_list[count]->geometry()); ChildBlockWidget->setGeometry(ChildBlockWidget->x()+this->x(),ChildBlockWidget->y()+this->y(),ChildBlockWidget->width(),ChildBlockWidget->height()); theBlockWidgets.append(ChildBlockWidget); } count--; } } return theBlockWidgets; }
void BlameMatrix::AddServerAlibi(uint server_idx, const QBitArray &bits) { Q_ASSERT(server_idx < _num_servers); Q_ASSERT(_num_users == static_cast<uint>(bits.count())); for(uint user_idx=0; user_idx<_num_users; user_idx++) { _data[user_idx][server_idx].server_bit= bits[user_idx]; } }
void TreeWidget::restoreState(const QByteArray& data) { QVariantMap state; QDataStream in(data); in >> state; if (state.contains("expanded")) { QBitArray expanded = state.value("expanded").toBitArray(); if (expanded.count() == topLevelItemCount()) { for (int i = 0; i < expanded.count(); ++i) topLevelItem(i)->setExpanded(expanded.testBit(i)); } } if (state.contains("sorting")) { d.sorting = state.value("sorting").toMap(); restoreSortOrder(); } }
bool FatherBlock::addStatus(QBitArray theStatus) { if(theStatus.count() == m_blocks_list.count()) { m_status_list.append(theStatus); return true; }else { return false; } }
void DashLabel::updateValue(QVariant value){ if(value.type() == QVariant::Double || value.type() == QVariant::String){ setText(value.toString()); }else if(value.type() == QVariant::BitArray){ QString text; QBitArray bit = value.toBitArray(); for(int i=0; i<bit.count(); i++){ text.append(bit[i]?'1':'0'); setText(text); } } }
void ReadBitsHelper(int arrlen, int offset, int n_bits) { QByteArray msg(arrlen, 'a'); QBitArray bits(n_bits, true); Serialization::WriteBitArray(bits, msg, offset); QBitArray out = Serialization::ReadBitArray(msg, offset, n_bits); EXPECT_EQ(n_bits, out.count()); for(int i=0; i<n_bits; i++) { EXPECT_EQ(true, out[i]); } }
void QtBinaryCalc::slot_test() { QBitArray *a = new QBitArray(); QBitArray *b = new QBitArray(); QBitArray *result = new QBitArray(); QString *resultString = new QString(); a->fill(0, 16); b->fill(0, 16); result->fill(0, 16); for(int i = 0 ; i < this->numberA->getBytesAsBin().count() ; i++) { a->setBit(i, this->numberA->getBytesAsBin().at(i).digitValue() ); } for(int i = 0 ; i < this->numberB->getBytesAsBin().count() ; i++) { b->setBit(i, this->numberB->getBytesAsBin().at(i).digitValue() ); } *result = ~(*a); SHOWVALUE(result, *result); for ( int i = result->count()-1 ; i >= 0 ; i-- ) { resultString->append( QString::number( (int)result->at(i) ) ); } SHOWVALUE(resultString, *resultString); SHOWVALUE(resultString->toInt(0, 2), resultString->toInt(0,2));//tu trzeba zrobic invert bitow 0->16, 1->15 itd this->numberResult->slot_setValue(resultString->toInt()); SHOWVALUE(a, *a); SHOWVALUE(b, *b); }
QList<QRect> FatherBlock::GetEnableBlockRects(int theStatusIndex,bool theRealBlocksFlag) { QList<QRect>theBlockRects; if(theStatusIndex == -1) { theStatusIndex = m_current_status_index; } if(theStatusIndex < m_status_list.count()) { QBitArray theStatus = m_status_list[theStatusIndex]; int count = theStatus.count()-1; while(count >= 0) { if(theStatus.testBit(count) == true) { QRect ChildBlockRect = QRect(m_blocks_list[count]->geometry()); ChildBlockRect.setX(ChildBlockRect.x() + this->x()); ChildBlockRect.setY(ChildBlockRect.y() + this->y()); ChildBlockRect.setWidth(m_blocks_list[count]->width()); ChildBlockRect.setHeight(m_blocks_list[count]->height()); if(!hasBottomBlock(count) && !theRealBlocksFlag) { QRect tempBlock(ChildBlockRect); tempBlock.setY(ChildBlockRect.y()+m_blocks_list[count]->height()); theBlockRects.append(tempBlock); } if(!hasRightBlock(count) && !theRealBlocksFlag) { QRect tempBlock(ChildBlockRect); tempBlock.setX(ChildBlockRect.x() + m_blocks_list[count]->width()); theBlockRects.append(tempBlock); } theBlockRects.append(ChildBlockRect); } count--; } } return theBlockRects; }
bool FatherBlock::RotateShape(int theStatusIndex) { if(theStatusIndex < m_status_list.count()) { QBitArray theStatus = m_status_list[theStatusIndex]; int count = theStatus.count()-1; while(count >= 0) { if(theStatus.testBit(count) == true) { SetStatusEnable(m_blocks_list[count]); }else { SetStatusDisable(m_blocks_list[count]); } count--; } return true; }else { return false; } }
TEST(Serialization, ReadBitsEasy) { QByteArray msg(1, 'a'); QBitArray bits(1, true); Serialization::WriteBitArray(bits, msg, 0); QBitArray out = Serialization::ReadBitArray(msg, 0, 1); ASSERT_EQ(1, out.count()); ASSERT_EQ(true, out[0]); bits.setBit(0, false); Serialization::WriteBitArray(bits, msg, 0); QBitArray out2 = Serialization::ReadBitArray(msg, 0, 1); ASSERT_EQ(1, out2.count()); ReadBitsHelper(2, 0, 8); ReadBitsHelper(2, 1, 8); ReadBitsHelper(2, 0, 9); ReadBitsHelper(3, 1, 9); ReadBitsHelper(3, 1, 10); ReadBitsHelper(3, 0, 20); }
void InputDaemon::modifyUnplugEvents(QQueue<SDL_Event> *sdlEventQueue) { QHashIterator<InputDevice*, InputDeviceBitArrayStatus*> genIter(releaseEventsGenerated); while (genIter.hasNext()) { genIter.next(); InputDevice *device = genIter.key(); InputDeviceBitArrayStatus *generatedTemp = genIter.value(); QBitArray tempBitArray = generatedTemp->generateFinalBitArray(); //qDebug() << "ARRAY: " << tempBitArray; unsigned int bitArraySize = tempBitArray.size(); //qDebug() << "ARRAY SIZE: " << bitArraySize; if (bitArraySize > 0 && tempBitArray.count(true) == bitArraySize) { if (pendingEventValues.contains(device)) { InputDeviceBitArrayStatus *pendingTemp = pendingEventValues.value(device); QBitArray pendingBitArray = pendingTemp->generateFinalBitArray(); QBitArray unplugBitArray = createUnplugEventBitArray(device); unsigned int pendingBitArraySize = pendingBitArray.size(); if (bitArraySize == pendingBitArraySize && pendingBitArray == unplugBitArray) { QQueue<SDL_Event> tempQueue; while (!sdlEventQueue->isEmpty()) { SDL_Event event = sdlEventQueue->dequeue(); switch (event.type) { case SDL_JOYBUTTONDOWN: case SDL_JOYBUTTONUP: { tempQueue.enqueue(event); break; } case SDL_JOYAXISMOTION: { if (event.jaxis.which != device->getSDLJoystickID()) { tempQueue.enqueue(event); } else { InputDevice *joy = trackjoysticks.value(event.jaxis.which); if (joy) { JoyAxis *axis = joy->getActiveSetJoystick()->getJoyAxis(event.jaxis.axis); if (axis) { if (axis->getThrottle() != JoyAxis::NormalThrottle) { event.jaxis.value = axis->getProperReleaseValue(); } } } tempQueue.enqueue(event); } break; } case SDL_JOYHATMOTION: { tempQueue.enqueue(event); break; } case SDL_CONTROLLERAXISMOTION: { if (event.caxis.which != device->getSDLJoystickID()) { tempQueue.enqueue(event); } else { InputDevice *joy = trackcontrollers.value(event.caxis.which); if (joy) { SetJoystick* set = joy->getActiveSetJoystick(); JoyAxis *axis = set->getJoyAxis(event.caxis.axis); if (axis) { if (event.caxis.axis == SDL_CONTROLLER_AXIS_TRIGGERLEFT || event.caxis.axis == SDL_CONTROLLER_AXIS_TRIGGERRIGHT) { event.caxis.value = axis->getProperReleaseValue(); } } } tempQueue.enqueue(event); } break; } case SDL_CONTROLLERBUTTONDOWN: case SDL_CONTROLLERBUTTONUP: { tempQueue.enqueue(event); break; } case SDL_JOYDEVICEREMOVED: case SDL_JOYDEVICEADDED: { tempQueue.enqueue(event); break; } default: { tempQueue.enqueue(event); } } } sdlEventQueue->swap(tempQueue); } } } } }
/// not threadsafe i64 DataFile::readScans(std::vector<int16> & scans_out, u64 pos, u64 num2read, const QBitArray & channelSubset, unsigned downSampleFactor) { if (pos > scanCt) return -1; if (num2read + pos > scanCt) num2read = scanCt - pos; QBitArray chset = channelSubset; if (chset.size() != (i64)nChans) chset.fill(true, nChans); if (downSampleFactor <= 0) downSampleFactor = 1; unsigned nChansOn = chset.count(true); int sizeofscans; if ( (num2read / downSampleFactor) * downSampleFactor < num2read ) sizeofscans = ((num2read / downSampleFactor)+1) * nChansOn; else sizeofscans = ((num2read / downSampleFactor)) * nChansOn; scans_out.resize(sizeofscans); u64 cur = pos; i64 nout = 0; std::vector<int> onChans; onChans.reserve(chset.size()); for (int i = 0, n = chset.size(); i < n; ++i) if (chset.testBit(i)) onChans.push_back(i); qint64 maxBufSize = nChans*sRate*1; // read about max 1sec worth of data at a time as an optimization qint64 desiredBufSize = num2read*nChans; // but first try and do the entire requested read at once in our buffer if it fits within our limits.. if (desiredBufSize > maxBufSize) desiredBufSize = maxBufSize; std::vector<int16> buf(desiredBufSize); if (int(buf.size()) < nChans) buf.resize(nChans); // minimum read is 1 scan while (cur < pos + num2read) { if (!dataFile.seek(cur * sizeof(int16) * nChans)) { Error() << "Error seeking in dataFile::readScans()!"; scans_out.clear(); return -1; } qint64 nr = dataFile.read(reinterpret_cast<char *>(&buf[0]), sizeof(int16) * buf.size()); if (nr < int(sizeof(int16) * nChans)) { Error() << "Short read in dataFile::readScans()!"; scans_out.clear(); return -1; } int nscans = int(nr/sizeof(int16))/nChans; if (nscans <= 0) { Error() << "Short read in dataFile::readScans()... nscans <= 0!"; scans_out.clear(); return -1; } const int16 *bufptr = &buf[0]; const int onChansSize = int(onChans.size()); const int skip = nChans*downSampleFactor; for (int sc = 0; sc < nscans && cur < pos + num2read; /* .. */) { if (int(nChansOn) == nChans) { i64 i_out = nout * nChans; for (int i = 0; i < nChans; ++i) scans_out[i_out + i] = int16(bufptr[i]); } else { // not all chans on, put subset 1 by 1 in the output vector i64 i_out = nout*i64(nChansOn); for (int i = 0; i < onChansSize; ++i) scans_out[i_out++] = int16(bufptr[onChans[i]]); } ++nout; bufptr += skip; sc += downSampleFactor; cur += downSampleFactor; } } return nout; }